Exemple #1
0
        private static bool TryUpdateReplicationDocument(this RavenHelper raven, Store store, Instance instance)
        {
            try
            {
                var destinations = raven.UpdateReplicationDocument(store, instance);
                raven.EnsureReplicationBundleIsActive(store, instance);
                logger.Info(
                    "Updated replication destinations for {0} to: [{1}]",
                    instance.Url,
                    string.Join(", ", destinations.Select(d => string.Format(
                                                              "{0}{1}",
                                                              d.Url,
                                                              d.TransitiveReplicationBehavior == TransitiveReplicationOptions.Replicate
                            ? " (transitive)"
                            : string.Empty))));
                return(true);
            }
            catch (Exception exception)
            {
                logger.ErrorException(
                    string.Format("Failed to update replication destinations for {0}.", instance.Url),
                    exception);

                if (exception.IsFatal())
                {
                    throw;
                }

                return(false);
            }
        }
Exemple #2
0
        private static void UpdateTracerDocument(this RavenHelper raven, Store store, Instance instance)
        {
            try
            {
                var tracerId = "brnkly/raven/tracers/" + instance.Url.Authority.Replace(":", "_");
                var docStore = raven.GetDocumentStore(instance.Url.GetServerRootUrl());
                using (var session = docStore.OpenSession(store.Name))
                {
                    session.Advanced.UseOptimisticConcurrency = true;
                    var tracer = session.Load <Tracer>(tracerId)
                                 ?? new Tracer {
                        UpdatedAtUtc = DateTimeOffset.UtcNow
                    };
                    tracer.UpdatedAtUtc = DateTimeOffset.UtcNow;
                    session.Store(tracer, tracerId);
                    session.SaveChanges();
                }

                logger.Debug("Tracer document updated for {0}", instance.Url);
            }
            catch (Exception exception)
            {
                logger.ErrorException(
                    string.Format("Failed to update tracer document for {0}", instance.Url),
                    exception);

                if (exception.IsFatal())
                {
                    throw;
                }
            }
        }
Exemple #3
0
        private static void EnsureReplicationBundleIsActive(this RavenHelper raven, Store store, Instance instance)
        {
            var docStore = raven.GetDocumentStore(instance.Url.GetServerRootUrl());

            docStore.DatabaseCommands.EnsureDatabaseExists(store.Name);
            using (var session = docStore.OpenSession())
            {
                session.Advanced.UseOptimisticConcurrency = true;
                var dbDoc         = session.Load <RavenJObject>("Raven/Databases/" + store.Name);
                var settings      = dbDoc["Settings"].Value <RavenJObject>();
                var activeBundles = settings[Constants.ActiveBundles] ?? "";
                var bundles       = activeBundles.Value <string>()
                                    .Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                                    .Select(x => x.Trim())
                                    .ToArray();
                if (!bundles.Contains("Replication"))
                {
                    var newActiveBundles = string.Join(
                        ";",
                        bundles.Concat(new[] { "Replication" }).ToArray());
                    settings[Constants.ActiveBundles] = newActiveBundles;
                    session.Store(dbDoc);
                    session.SaveChanges();
                }
            }
        }
Exemple #4
0
        private static void RequestData(
            this RavenHelper raven,
            Store store,
            Dictionary <Uri, Task <DatabaseStatistics> > dbStats,
            Dictionary <Uri, Task <RavenJToken> > replicationStats,
            Dictionary <Uri, Task <IndexDefinition[]> > indexes)
        {
            foreach (var instance in store.Instances)
            {
                var docStore = raven.GetDocumentStore(instance.Url.GetServerRootUrl());

                using (var session = docStore.OpenAsyncSession())
                {
                    dbStats.Add(
                        instance.Url,
                        session.Advanced.DocumentStore.AsyncDatabaseCommands.ForDatabase(store.Name)
                        .GetStatisticsAsync());

                    indexes.Add(
                        instance.Url,
                        session.Advanced.DocumentStore.AsyncDatabaseCommands.ForDatabase(store.Name)
                        .GetIndexesAsync(0, 255));

                    var asyncServerClient = session.Advanced.DocumentStore.AsyncDatabaseCommands
                                            .ForDatabase(store.Name) as AsyncServerClient;
                    replicationStats.Add(
                        instance.Url,
                        asyncServerClient
                        .CreateRequest("/replication/info?noCache=" + Guid.NewGuid(), "GET")
                        .ReadResponseJsonAsync());
                }
            }
        }
Exemple #5
0
 public static void DeleteIndex(this RavenHelper raven, Uri instanceUrl, string indexName)
 {
     try
     {
         raven.GetDatabaseCommands(instanceUrl).DeleteIndex(indexName);
     }
     catch (Exception exception)
     {
     }
 }
Exemple #6
0
        public static void UpdateTracerDocuments(this RavenHelper raven, Store store)
        {
            var tasks = new List <Task>();

            foreach (var instance in store.Instances)
            {
                var task = new Task(() => raven.UpdateTracerDocument(store, instance));
                tasks.Add(task);
                task.Start();
            }

            Task.WaitAll(tasks.ToArray(), TimeSpan.FromSeconds(5));
        }
Exemple #7
0
        public static async Task <StoreStats> GetStats(this RavenHelper raven, Store store)
        {
            var dbStats          = new Dictionary <Uri, Task <DatabaseStatistics> >();
            var replicationStats = new Dictionary <Uri, Task <RavenJToken> >();
            var indexes          = new Dictionary <Uri, Task <IndexDefinition[]> >();

            raven.RequestData(store, dbStats, replicationStats, indexes);
            var responses = await GatherResponses(store, dbStats, replicationStats, indexes);

            var storeStats = CreateStats(store, responses);

            return(storeStats);
        }
Exemple #8
0
        public static void Copy(this RavenHelper raven, Uri fromInstanceUrl, Uri toInstanceUrl, string indexName)
        {
            var source      = raven.GetDatabaseCommands(fromInstanceUrl);
            var destination = raven.GetDatabaseCommands(toInstanceUrl);

            var sourceIndexDefinition = source.GetIndex(indexName);

            if (sourceIndexDefinition == null)
            {
                throw new InvalidOperationException("Index definition is null.");
            }

            destination.PutIndex(indexName, sourceIndexDefinition, overwrite: true);
        }
Exemple #9
0
        public static IEnumerable <string> UpdateReplicationDocuments(this RavenHelper raven, Store store)
        {
            var results = new Collection <string>();

            foreach (var instance in store.Instances)
            {
                if (!raven.TryUpdateReplicationDocument(store, instance))
                {
                    results.Add(
                        string.Format(
                            "Failed to update replication destinations for {0}",
                            instance.Url));
                }
            }

            return(results);
        }
Exemple #10
0
        private static IEnumerable <ReplicationDestination> UpdateReplicationDocument(this RavenHelper raven, Store store, Instance instance)
        {
            var docStore = raven.GetDocumentStore(instance.Url.GetServerRootUrl());

            docStore.DatabaseCommands.EnsureDatabaseExists(store.Name);
            using (var session = docStore.OpenSession(store.Name))
            {
                session.Advanced.UseOptimisticConcurrency = true;
                var replicationDoc =
                    session.Load <ReplicationDocument>(Constants.RavenReplicationDestinations) ??
                    new ReplicationDocument();
                replicationDoc.Destinations = instance.Destinations
                                              .Where(d => !d.Url.Equals(instance.Url.ToString(), StringComparison.OrdinalIgnoreCase))
                                              .ToList();
                session.Store(replicationDoc);
                session.SaveChanges();

                return(replicationDoc.Destinations);
            }
        }
Exemple #11
0
        private static IDatabaseCommands GetDatabaseCommands(this RavenHelper helper, Uri instanceUrl)
        {
            var docStore = helper.GetDocumentStore(instanceUrl.GetServerRootUrl());

            return(docStore.DatabaseCommands.ForDatabase(instanceUrl.GetDatabaseName()));
        }
Exemple #12
0
 public static void ResetIndex(this RavenHelper raven, Uri instanceUrl, string indexName)
 {
     raven.GetDatabaseCommands(instanceUrl).ResetIndex(indexName);
 }