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);
		}
		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
			});
		}
Beispiel #3
0
		public AlertProxy(Alert alert)
		{
			Alert = alert;
			Title = alert.Title;
			CreatedAt = alert.CreatedAt;
			Observed = new Observable<bool> {Value = alert.Observed};
			Message = alert.Message;
			AlertLevel = alert.AlertLevel;

			Observed.PropertyChanged += (sender, args) =>
			{
				Alert.Observed = Observed.Value;
			};
		}
Beispiel #4
0
        public static void AddAlert(this DocumentDatabase self, Alert alert)
        {
            while (true)
            {
                using (self.TransactionalStorage.DisableBatchNesting())
                using (var putSerialLock = self.DocumentLock.TryLock(25))
                {
                    if (putSerialLock == null)
                        continue;

                    AlertsDocument alertsDocument;
                    var alertsDoc = self.Documents.Get(Constants.RavenAlerts, null);
                    RavenJObject metadata;
                    Etag etag;
                    if (alertsDoc == null)
                    {
                        etag = Etag.Empty;
                        alertsDocument = new AlertsDocument();
                        metadata = new RavenJObject();
                    }
                    else
                    {
                        etag = alertsDoc.Etag;
                        alertsDocument = alertsDoc.DataAsJson.JsonDeserialization<AlertsDocument>() ?? new AlertsDocument();
                        metadata = alertsDoc.Metadata;
                    }

                    var withSameUniqe = alertsDocument.Alerts.FirstOrDefault(alert1 => alert1.UniqueKey == alert.UniqueKey);
                    if (withSameUniqe != null)
                    {
                        // copy information about observed
                        alert.LastDismissedAt = withSameUniqe.LastDismissedAt;
                        alertsDocument.Alerts.Remove(withSameUniqe);
                    }

                    alertsDocument.Alerts.Add(alert);
                    var document = RavenJObject.FromObject(alertsDocument);
                    document.Remove("Id");
                    try
                    {
                        self.Documents.Put(Constants.RavenAlerts, etag, document, metadata, null);
                        return;
                    }
                    catch (ConcurrencyException)
                    {
                        //try again...
                    }
                }
            }
        }
		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 AddAlert(Alert alert)
		{
			lock (putSerialLock)
			{
				AlertsDocument alertsDocument;
				var alertsDoc = Get(Constants.RavenAlerts, null);
				RavenJObject metadata;
				if (alertsDoc == null)
				{
					alertsDocument = new AlertsDocument();
					metadata = new RavenJObject();
				}
				else
				{
					alertsDocument = alertsDoc.DataAsJson.JsonDeserialization<AlertsDocument>() ?? new AlertsDocument();
					metadata = alertsDoc.Metadata;
				}

				var withSameUniqe = alertsDocument.Alerts.FirstOrDefault(alert1 => alert1.UniqueKey == alert.UniqueKey);
				if (withSameUniqe != null)
					alertsDocument.Alerts.Remove(withSameUniqe);

				alertsDocument.Alerts.Add(alert);
				var document = RavenJObject.FromObject(alertsDocument);
				document.Remove("Id");
				Put(Constants.RavenAlerts, null, document, metadata, null);
			}
		}
Beispiel #7
0
		public RelationalDatabaseWriter.TableQuerySummary[] SimulateSqlReplicationSqlQueries(string strDocumentId, SqlReplicationConfig sqlReplication, bool performRolledbackTransaction, out Alert alert)
		{
			RelationalDatabaseWriter.TableQuerySummary[] resutls = null;

			try
			{
				var stats = new SqlReplicationStatistics(sqlReplication.Name, false);

				var jsonDocument = Database.Documents.Get(strDocumentId, null);
				JsonDocument.EnsureIdInMetadata(jsonDocument);
				var doc = jsonDocument.ToJson();
				doc[Constants.DocumentIdFieldName] = jsonDocument.Key;

				var docs = new List<ReplicatedDoc>
                           {
	                           new ReplicatedDoc
	                           {
		                           Document = doc,
								   Etag = jsonDocument.Etag,
								   Key = jsonDocument.Key,
								   SerializedSizeOnDisk = jsonDocument.SerializedSizeOnDisk
	                           }
                           };
				var scriptResult = ApplyConversionScript(sqlReplication, docs, stats);

				var connectionsDoc = Database.Documents.Get(Constants.RavenSqlReplicationConnectionsDocumentName, null);
				var sqlReplicationConnections = connectionsDoc != null ? connectionsDoc.DataAsJson.JsonDeserialization<SqlReplicationConnections>() : new SqlReplicationConnections();

				if (PrepareSqlReplicationConfig(sqlReplication, sqlReplication.Name, stats, sqlReplicationConnections, false, false))
				{
					if (performRolledbackTransaction)
					{
						using (var writer = new RelationalDatabaseWriter(Database, sqlReplication, stats))
						{
							resutls = writer.RolledBackExecute(scriptResult).ToArray();
						}
					}
					else
					{
						var simulatedwriter = new RelationalDatabaseWriterSimulator(Database, sqlReplication, stats);
						resutls = new List<RelationalDatabaseWriter.TableQuerySummary>
                        {
                            new RelationalDatabaseWriter.TableQuerySummary
                            {
                                Commands = simulatedwriter.SimulateExecuteCommandText(scriptResult)
                                    .Select(x => new RelationalDatabaseWriter.TableQuerySummary.CommandData
                                    {
                                        CommandText = x
                                    }).ToArray()
                            }
                        }.ToArray();


					}
				}

				alert = stats.LastAlert;
			}
			catch (Exception e)
			{
				alert = new Alert
				{
					AlertLevel = AlertLevel.Error,
					CreatedAt = SystemTime.UtcNow,
					Message = "Last SQL replication operation for " + sqlReplication.Name + " was failed",
					Title = "SQL replication error",
					Exception = e.ToString(),
					UniqueKey = "Sql Replication Error: " + sqlReplication.Name
				};
			}
			return resutls;
		}
		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);
		}
Beispiel #9
0
		public void AddAlert(Alert alert)
		{
			AlertsDocument alertsDocument;
			var alertsDoc = Get(Constants.RavenAlerts, null);
			if (alertsDoc == null)
				alertsDocument = new AlertsDocument();
			else
				alertsDocument = alertsDoc.DataAsJson.JsonDeserialization<AlertsDocument>() ?? new AlertsDocument();

			alertsDocument.Alerts.Add(alert);
		}
		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);
		}
Beispiel #11
0
        public RelationalDatabaseWriter.TableQuerySummary[] SimulateSqlReplicationSQLQueries(string strDocumentId, SqlReplicationConfig sqlReplication, bool performRolledbackTransaction, out Alert alert)
        {
            alert = null;
            RelationalDatabaseWriter.TableQuerySummary[] resutls = null;
            
            try
            {
                var stats = new SqlReplicationStatistics(sqlReplication.Name);
                var docs = new List<JsonDocument>() { Database.Documents.Get(strDocumentId, null) };
                var scriptResult = ApplyConversionScript(sqlReplication, docs);

                var connectionsDoc = Database.Documents.Get(connectionsDocumentName, null);
                var sqlReplicationConnections = connectionsDoc.DataAsJson.JsonDeserialization<SqlReplicationConnections>();

                if (PrepareSqlReplicationConfig( sqlReplication, sqlReplication.Name,  stats, sqlReplicationConnections, false,false))
                {
                    if (performRolledbackTransaction)
                    {
                        using (var writer = new RelationalDatabaseWriter(Database, sqlReplication, stats))
                        {
                            resutls = writer.RolledBackExecute(scriptResult).ToArray();
                        }
                    }
                    else
                    {
                        var simulatedwriter = new RelationalDatabaseWriterSimulator(Database, sqlReplication, stats);
                        resutls = new List<RelationalDatabaseWriter.TableQuerySummary>()
                        {
                            new RelationalDatabaseWriter.TableQuerySummary()
                            {
                                Commands = simulatedwriter.SimulateExecuteCommandText(scriptResult)
                                    .Select(x => new RelationalDatabaseWriter.TableQuerySummary.CommandData()
                                    {
                                        CommandText = x
                                    }).ToArray()
                            }
                        }.ToArray();


                    }
                }

                alert = stats.LastAlert;
            }
            catch (Exception e)
            {
                alert = new Alert()
                {
                    AlertLevel = AlertLevel.Error,
                    CreatedAt = SystemTime.UtcNow,
                    Message = "Last SQL replication operation for " + sqlReplication.Name + " was failed",
                    Title = "SQL replication error",
                    Exception = e.ToString(),
                    UniqueKey = "Sql Replication Error: " + sqlReplication.Name
                };
            }
            return resutls;
        }