private void HandleSlowSql(long elapsedMiliseconds, string stmt) { var message = string.Format("Slow SQL detected. Execution took: {0}ms, statement: {1}", elapsedMiliseconds, stmt); log.Warn(message); database.AddAlert(new Alert { AlertLevel = AlertLevel.Warning, CreatedAt = SystemTime.UtcNow, Message = message, Title = "Slow SQL statement", UniqueKey = "Slow SQL statement" }); }
public void RecordWriteError(Exception e, DocumentDatabase database, int count = 1, DateTime?newErrorTime = null) { WriteErrorCount += count; if (WriteErrorCount < 100) { return; } if (WriteErrorCount <= SuccessCount) { return; } if (newErrorTime != null) { LastErrorTime = newErrorTime.Value; return; } database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication write error hit ratio too high", Exception = e.ToString(), UniqueKey = "Sql Replication Write Error Ratio: " + name }); throw new InvalidOperationException("Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, e); }
public void RecordScriptError(DocumentDatabase database) { ScriptErrorCount++; if (ScriptErrorCount < 100) { return; } if (ScriptErrorCount <= ScriptSuccessCount) { return; } database.AddAlert(LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication script error hit ratio too high", UniqueKey = "Sql Replication Script Error Ratio: " + name }); throw new InvalidOperationException("Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this); }
public void RecordWriteError(Exception e, DocumentDatabase database, int count = 1, DateTime? newErrorTime = null) { WriteErrorCount += count; if (WriteErrorCount < 100) return; if (WriteErrorCount <= SuccessCount) return; if (newErrorTime != null) { LastErrorTime = newErrorTime.Value; return; } database.AddAlert(LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication write error hit ratio too high", Exception = e.ToString(), UniqueKey = "Sql Replication Write Error Ratio: " + name }); throw new InvalidOperationException("Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, e); }
private DbProviderFactory GetDbProviderFactory(SqlReplicationConfig cfg) { DbProviderFactory providerFactory; try { providerFactory = DbProviderFactories.GetFactory(cfg.FactoryName); } catch (Exception e) { log.WarnException( string.Format("Could not find provider factory {0} to replicate to sql for {1}, ignoring", cfg.FactoryName, cfg.Name), e); database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication Count not find factory provider", Message = string.Format("Could not find factory provider {0} to replicate to sql for {1}, ignoring", cfg.FactoryName, cfg.Name), UniqueKey = string.Format("Sql Replication Provider Not Found: {0}, {1}", cfg.Name, cfg.FactoryName) }); throw; } return(providerFactory); }
public ElasticsearchDestinationWriter(DocumentDatabase database, SqlReplicationConfig _cfg, SqlReplicationStatistics replicationStatistics) { var cfg = new ElasticsearchReplicationConfig(_cfg); this.database = database; this.cfg = cfg; this.targetIndexName = cfg.FactoryName.ToLowerInvariant(); // Elasticsearch requires all index names to be lowercased this.replicationStatistics = replicationStatistics; try { elasticsearchClient = cfg.GetElasticClient(); } catch (UriFormatException e) { if (database != null) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Invalid Elasticsearch URL provided", Message = "Elasticsearch Replication could not parse one of the provided node URLs", UniqueKey = "Elasticsearch Replication Connection Error: " + cfg.ConnectionString }); } } catch (Exception e) { if (database != null) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Elasticsearch Replication could not open connection", Message = "Elasticsearch Replication could not open connection to " + cfg.ConnectionString, UniqueKey = "Elasticsearch Replication Connection Error: " + cfg.ConnectionString }); } throw; } }
private ReplicationStrategy[] GetReplicationDestinations() { var document = docDb.Get(Constants.RavenReplicationDestinations, null); if (document == null) { return(new ReplicationStrategy[0]); } ReplicationDocument jsonDeserialization; try { jsonDeserialization = document.DataAsJson.JsonDeserialization <ReplicationDocument>(); } catch (Exception e) { log.Warn("Cannot get replication destinations", e); return(new ReplicationStrategy[0]); } if (string.IsNullOrWhiteSpace(jsonDeserialization.Source)) { jsonDeserialization.Source = docDb.TransactionalStorage.Id.ToString(); try { var ravenJObject = RavenJObject.FromObject(jsonDeserialization); ravenJObject.Remove("Id"); docDb.Put(Constants.RavenReplicationDestinations, document.Etag, ravenJObject, document.Metadata, null); } catch (ConcurrencyException) { // we will get it next time } } if (jsonDeserialization.Source != docDb.TransactionalStorage.Id.ToString()) { docDb.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Source of the ReplicationDestinations document is not the same as the database it is located in", Title = "Wrong replication source: " + jsonDeserialization.Source + " instead of " + docDb.Name, UniqueKey = "Wrong source: " + jsonDeserialization.Source + ", " + docDb.TransactionalStorage.Id.ToString() }); return(new ReplicationStrategy[0]); } return(jsonDeserialization .Destinations .Where(x => !x.Disabled) .Select(GetConnectionOptionsSafe) .Where(x => x != null) .ToArray()); }
public void MarkScriptAsInvalid(DocumentDatabase database, string script) { ScriptErrorCount = int.MaxValue; LastErrorTime = DateTime.MaxValue; database.AddAlert(LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = string.Format("Could not parse script for {0} " + Environment.NewLine + "Script: {1}", name, script), Title = "Could not parse Script", UniqueKey = "Script Parse Error: " + name }); }
public void RecordWriteError(Exception e, DocumentDatabase database, int count = 1, DateTime?suspendUntil = null) { WriteErrorCount += count; LastErrorTime = SystemTime.UtcNow; LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Last SQL eplication operation for " + name + " was failed", Title = "SQL replication error", Exception = e.ToString(), UniqueKey = "Sql Replication Error: " + name }; if (WriteErrorCount < 100) { return; } if (WriteErrorCount <= SuccessCount) { return; } if (suspendUntil != null) { SuspendUntil = suspendUntil.Value; return; } LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication write error hit ratio too high", Exception = e.ToString(), UniqueKey = "Sql Replication Write Error Ratio: " + name }; if (reportToDatabaseAlerts) { database.AddAlert(LastAlert); } throw new InvalidOperationException("Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, e); }
public RelationalDatabaseWriter(DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; if (SqlServerFactoryNames.Contains(cfg.FactoryName)) { IsSqlServerFactoryType = true; } try { connection.Open(); } catch (Exception e) { var message = "Sql Replication could not open connection to " + connection.ConnectionString; log.Error(message); database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = message, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); stringParserList = GenerateStringParsers(); sqlReplicationMetrics = database.StartupTasks.OfType <SqlReplicationTask>().FirstOrDefault().GetSqlReplicationMetricsManager(cfg); }
public void RecordWriteError(Exception e, DocumentDatabase database, int count = 1, DateTime? suspendUntil = null) { WriteErrorCount += count; LastErrorTime = SystemTime.UtcNow; LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Last SQL eplication operation for " + name + " was failed", Title = "SQL replication error", Exception = e.ToString(), UniqueKey = "Sql Replication Error: " + name }; if (WriteErrorCount < 100) return; if (WriteErrorCount <= SuccessCount) return; if (suspendUntil != null) { SuspendUntil = suspendUntil.Value; return; } LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication write error hit ratio too high", Exception = e.ToString(), UniqueKey = "Sql Replication Write Error Ratio: " + name }; if (reportToDatabaseAlerts) { database.AddAlert(LastAlert); } throw new InvalidOperationException("Could not tolerate write error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, e); }
public void RecordScriptError(DocumentDatabase database, Exception e) { ScriptErrorCount++; LastErrorTime = SystemTime.UtcNow; LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Replication script for " + name + " was failed", Title = "SQL replication error", Exception = e.ToString(), UniqueKey = "Sql Replication Error: " + name }; if (ScriptErrorCount < 100) { return; } if (ScriptErrorCount <= ScriptSuccessCount) { return; } LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication script error hit ratio too high", UniqueKey = "Sql Replication Script Error Ratio: " + name }; if (reportToDatabaseAlerts) { database.AddAlert(LastAlert); } throw new InvalidOperationException("Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this); }
public RelationalDatabaseWriter(DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; try { connection.Open(); } catch (Exception e) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = "Sql Replication could not open connection to " + connection.ConnectionString, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); }
public RelationalDatabaseWriter( DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; try { connection.Open(); } catch (Exception e) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = "Sql Replication could not open connection to " + connection.ConnectionString, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); }
public void RecordScriptError(DocumentDatabase database) { ScriptErrorCount++; if (ScriptErrorCount < 100) return; if (ScriptErrorCount <= ScriptSuccessCount) return; database.AddAlert(LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication script error hit ratio too high", UniqueKey = "Sql Replication Script Error Ratio: " + name }); throw new InvalidOperationException("Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this); }
public void RecordScriptError(DocumentDatabase database, Exception e) { ScriptErrorCount++; LastErrorTime = SystemTime.UtcNow; LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Replication script for " + name + " was failed", Title = "SQL replication error", Exception = e.ToString(), UniqueKey = "Sql Replication Error: " + name }; if (ScriptErrorCount < 100) return; if (ScriptErrorCount <= ScriptSuccessCount) return; LastAlert = new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Message = "Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this, Title = "Sql Replication script error hit ratio too high", UniqueKey = "Sql Replication Script Error Ratio: " + name }; if (reportToDatabaseAlerts) { database.AddAlert(LastAlert); } throw new InvalidOperationException("Could not tolerate script error ratio and stopped current replication cycle for " + name + Environment.NewLine + this); }
public RelationalDatabaseWriter( DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; try { connection.Open(); } catch (Exception e) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = "Sql Replication could not open connection to " + connection.ConnectionString, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); stringParserList = new List<Func<DbParameter, string, bool>> { (colParam, value) => { if( char.IsDigit( value[ 0 ] ) ) { DateTime dateTime; if (DateTime.TryParseExact(value, Default.OnlyDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime)) { switch( providerFactory.GetType( ).Name ) { case "MySqlClientFactory": colParam.Value = dateTime.ToString( "yyyy-MM-dd HH:mm:ss.ffffff" ); break; default: colParam.Value = dateTime; break; } return true; } } return false; }, (colParam, value) => { if( char.IsDigit( value[ 0 ] ) ) { DateTimeOffset dateTimeOffset; if( DateTimeOffset.TryParseExact( value, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset ) ) { switch( providerFactory.GetType( ).Name ) { case "MySqlClientFactory": colParam.Value = dateTimeOffset.ToUniversalTime().ToString( "yyyy-MM-dd HH:mm:ss.ffffff" ); break; default: colParam.Value = dateTimeOffset; break; } return true; } } return false; } }; }
public RelationalDatabaseWriter( DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; if (SqlServerFactoryNames.Contains(cfg.FactoryName)) { IsSqlServerFactoryType = true; } try { connection.Open(); } catch (Exception e) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = "Sql Replication could not open connection to " + connection.ConnectionString, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); stringParserList = GenerateStringParsers(); sqlReplicationMetrics = database.StartupTasks.OfType<SqlReplicationTask>().FirstOrDefault().GetSqlReplicationMetricsManager(cfg); }
public RelationalDatabaseWriter(DocumentDatabase database, SqlReplicationConfig cfg, SqlReplicationStatistics replicationStatistics) { this.database = database; this.cfg = cfg; this.replicationStatistics = replicationStatistics; providerFactory = GetDbProviderFactory(cfg); commandBuilder = providerFactory.CreateCommandBuilder(); connection = providerFactory.CreateConnection(); Debug.Assert(connection != null); Debug.Assert(commandBuilder != null); connection.ConnectionString = cfg.ConnectionString; try { connection.Open(); } catch (Exception e) { database.AddAlert(new Alert { AlertLevel = AlertLevel.Error, CreatedAt = SystemTime.UtcNow, Exception = e.ToString(), Title = "Sql Replication could not open connection", Message = "Sql Replication could not open connection to " + connection.ConnectionString, UniqueKey = "Sql Replication Connection Error: " + connection.ConnectionString }); throw; } tx = connection.BeginTransaction(); stringParserList = new List <Func <DbParameter, string, bool> > { (colParam, value) => { if (char.IsDigit(value[0])) { DateTime dateTime; if (DateTime.TryParseExact(value, Default.OnlyDateTimeFormat, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTime)) { switch (providerFactory.GetType( ).Name) { case "MySqlClientFactory": colParam.Value = dateTime.ToString("yyyy-MM-dd HH:mm:ss.ffffff"); break; default: colParam.Value = dateTime; break; } return(true); } } return(false); }, (colParam, value) => { if (char.IsDigit(value[0])) { DateTimeOffset dateTimeOffset; if (DateTimeOffset.TryParseExact(value, Default.DateTimeFormatsToRead, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind, out dateTimeOffset)) { switch (providerFactory.GetType( ).Name) { case "MySqlClientFactory": colParam.Value = dateTimeOffset.ToUniversalTime().ToString("yyyy-MM-dd HH:mm:ss.ffffff"); break; default: colParam.Value = dateTimeOffset; break; } return(true); } } return(false); } }; }