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; } }
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()); } } }
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(); } } }
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; }
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)); }
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; }
private static async Task<List<InstanceStatsHolder>> GatherResponses( Store store, Dictionary<Uri, Task<DatabaseStatistics>> dbStats, Dictionary<Uri, Task<RavenJToken>> replicationStats, Dictionary<Uri, Task<IndexDefinition[]>> indexes) { var responses = new List<InstanceStatsHolder>(); foreach (var instance in store.Instances) { var response = new InstanceStatsHolder { Url = instance.Url }; try { response.DatabaseStats = await dbStats[instance.Url]; var indexData = await indexes[instance.Url]; response.IndexHashCodes = indexData.ToDictionary( i => i.Name, i => i.GetHashCode()); var data = await replicationStats[instance.Url]; response.ReplicationStats = new RavenImports.JsonSerializer() .Deserialize<ReplicationStatistics>(new RavenJTokenReader(data)); } catch (Exception exception) { if (exception.IsFatal()) { throw; } // TODO: Log exception. response.DatabaseStats = response.DatabaseStats ?? new DatabaseStatistics(); response.ReplicationStats = response.ReplicationStats ?? new ReplicationStatistics { Self = response.Url.ToString() }; } responses.Add(response); } return responses; }
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; } } }
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; } }
private static StoreStats CreateStats(Store store, IEnumerable<InstanceStatsHolder> responses) { var storeStats = new StoreStats { Name = store.Name }; AddStoreIndexStats(storeStats, responses); foreach (var instance in store.Instances) { var response = responses.Where(r => r.Url == instance.Url).FirstOrDefault(); AddInstanceIndexStats(storeStats, response); storeStats.Replication.Add(response.ReplicationStats); } return storeStats; }