public void RebuildTableIndexes(string tableName, string indexName, SqlConnection connection) { try { Log.Info("[DistributedDb] RebuildTableIndexes Start - [" + tableName + "] ON " + indexName, this); connection.Open(); var command = @" ALTER INDEX " + indexName + @" ON " + tableName + @" REBUILD WITH (FILLFACTOR = 80); "; using (var sqlCommand = new SqlCommand(command, connection)) { //sqlCommand.CommandTimeout = 30; SyncTracer.Info("Rebuild index table " + indexName + " ON " + tableName); SyncTracerExtended.TraceCommandAndParameters((IDbCommand)sqlCommand); sqlCommand.ExecuteNonQuery(); } connection.Close(); Log.Info("[DistributedDb] RebuildTableIndexes End - [" + tableName + "] ON " + indexName, this); } catch (Exception ex) { if (connection.State == ConnectionState.Open) { connection.Close(); } Log.Error("[DistributedDb] RebuildTableIndexes Error - [" + tableName + "] ON " + indexName, ex, this); } }
static void Main() { SyncTracer.Info("Starting Application"); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new SyncForm()); }
public void TruncateTable(string tableName, SqlConnection connection) { try { Log.Info("[DistributedDb] TruncateTable Start - [" + tableName + "]", this); connection.Open(); var command = @" TRUNCATE TABLE " + tableName + @"; "; using (var sqlCommand = new SqlCommand(command, connection)) { SyncTracer.Info("Truncate table " + tableName); SyncTracerExtended.TraceCommandAndParameters((IDbCommand)sqlCommand); sqlCommand.ExecuteNonQuery(); } connection.Close(); Log.Info("[DistributedDb] TruncateTable End - " + tableName + "]", this); } catch (Exception ex) { if (connection.State == ConnectionState.Open) { connection.Close(); } Log.Error("[DistributedDb] TruncateTable Error - " + tableName + "]", ex, this); } }
/// <summary> /// Read and parse the incoming request details for a DownloadChanges GET request. /// </summary> private void ReadIncomingRequestStreamForGet() { _syncBlob = null; // The syncblob is in the headers or querystring for a get request and there are no entities sent from the client. // Check the query string for the server blob since it overrides the header. string syncServerBlobQueryValue = _serviceHost.QueryStringCollection[SyncServiceConstants.SYNC_SERVERBLOB_QUERYKEY]; if (!String.IsNullOrEmpty(syncServerBlobQueryValue) && syncServerBlobQueryValue.Trim().Length > 0) { SyncTracer.Verbose("Server blob read from query string for HTTP GET: {0}", syncServerBlobQueryValue); _syncBlob = Convert.FromBase64String(syncServerBlobQueryValue.Trim()); } // Check the request headers for the blob. else if (!String.IsNullOrEmpty(_serviceHost.RequestHeaders.Get(SyncServiceConstants.SYNC_SERVERBLOB_HEADERKEY)) && _serviceHost.RequestHeaders[SyncServiceConstants.SYNC_SERVERBLOB_HEADERKEY].Trim().Length > 0) { SyncTracer.Verbose("Server blob read from request headers for HTTP GET: {0}", syncServerBlobQueryValue); _syncBlob = Convert.FromBase64String(_serviceHost.RequestHeaders[SyncServiceConstants.SYNC_SERVERBLOB_HEADERKEY].Trim()); } else { SyncTracer.Info("No server blob in query string or header."); } }
/// <summary> /// This method tries to look up the service configuration from the MetadataCache. /// For the first request, there will be no item in the cache and so, an object of type /// SyncServiceConfiguration is created, initialized and added to the MetadataCache. /// </summary> private void CreateConfiguration(string scope) { Type serviceType = base.GetType(); // Check if we already have a configuration for the service in the metadata cache. //MetadataCacheItem item = MetadataCache.TryLookup(serviceType); MetadataCacheItem item = MetadataCache.TryLookup(serviceType); if (null == item) { SyncTracer.Info("Creating SyncServiceConfiguration for service type {0}", serviceType); item = new MetadataCacheItem(serviceType); // Initialize the private member since it will then have default values. // In case of an error in the static initializer, we can refer to the default values // of configuration. _syncConfiguration = new SyncServiceConfiguration(typeof(T)); // This will invoke the static initialization method. _syncConfiguration.Initialize(serviceType, scope); String conflictPolicy = Common.Logon.GetConflictPolicy(scope); if (!String.IsNullOrEmpty(conflictPolicy)) { String p = conflictPolicy.ToLower(); switch (p) { case "serverwins": _syncConfiguration.SetConflictResolutionPolicy(ConflictResolutionPolicy.ServerWins); break; case "clientwins": _syncConfiguration.SetConflictResolutionPolicy(ConflictResolutionPolicy.ClientWins); break; default: throw new Exception("Invalid conflictPolicy value"); } } item.Configuration = _syncConfiguration; //MetadataCache.AddCacheItem(serviceType, item); MetadataCache.AddCacheItem(serviceType, item); SyncTracer.Info("SyncServiceConfiguration for service type {0} created successfully!", serviceType); } else { _syncConfiguration = item.Configuration; } // Invoke the testhook Initialize method. // Note: This needs to be called regardless of whether we find the configuration in the // cache or not because this is on a per request basis. _syncConfiguration.InvokeTestHookInitializeMethod(serviceType); }
internal static void TryOpenConnection(IDbConnection connection) { for (int index = 0; index < 6; ++index) { try { if (index > 0) { SyncTracer.Info("Retrying opening connection, attempt {0} of {1}.", new object[2] { (object)index, (object)5 }); } connection.Open(); SqlConnection connection1 = connection as SqlConnection; if (connection1 == null) { break; } using (SqlCommand sqlCommand = new SqlCommand("Select 1", connection1)) { sqlCommand.ExecuteScalar(); break; } } catch (SqlException ex) { if (index == 5) { SyncTracer.Error("Open connection failed after max retry attempts, due to exception: {0}", new object[1] { (object)ex.Message }); throw; } else if (!SyncUtil.RetryLitmus(ex)) { SyncTracer.Error("Open connection failed on attempt {0} of {1}, due to unretryable exception: {2}", (object)(index + 1), (object)5, (object)ex.Message); throw; } else { SyncTracer.Warning("Open connection failed on attempt {0} of {1}, due to retryable exception: {2}", (object)(index + 1), (object)5, (object)ex.Message); Thread.Sleep(100 * (int)Math.Pow(2.0, (double)index)); } } } }
/// <summary> /// Read request details for Download/upload operations. /// </summary> private void ReadIncomingRequestDetails() { if (0 == String.Compare(_serviceHost.RequestHttpMethod, SyncServiceConstants.HTTP_VERB_GET, true)) { SyncTracer.Info("Request HTTP method is GET"); ReadIncomingRequestStreamForGet(); } else { SyncTracer.Info("Request HTTP method is POST"); // Parse request stream and populate members. ReadIncomingRequestStreamForPost(); } }
internal static void ExecuteNonQueryWithNewTransaction(IDbCommand command) { for (int index = 0; index < 6; ++index) { try { if (index > 0) { SyncTracer.Info("Retrying SyncUtil.ExecuteNonQueryWithNewTransaction, attempt {0} of {1}.", new object[2] { (object)index, (object)5 }); SyncUtil.OpenConnection(command.Connection); } using (IDbTransaction dbTransaction = command.Connection.BeginTransaction()) { command.Transaction = dbTransaction; command.ExecuteNonQuery(); dbTransaction.Commit(); break; } } catch (DbException ex) { if (index == 5) { SyncTracer.Error("SyncUtil.ExecuteNonQueryWithNewTransaction failed after max retry attempts, due to exception: {0}", new object[1] { (object)ex.Message }); throw; } else { SyncTracer.Warning("SyncUtil.ExecuteNonQueryWithNewTransaction failed on attempt {0} of {1}, due to retryable exception: {2}", (object)index, (object)5, (object)ex.Message); Thread.Sleep(100 * (int)Math.Pow(2.0, (double)index)); } } } }
public void UpdateTriggerScripts(string tableName, string cmdText, SqlConnection connection) { try { connection.Open(); using (var sqlCommand = new SqlCommand(cmdText, connection)) { //sqlCommand.CommandTimeout = 30; SyncTracer.Info("Update Trigger GETDATE() to GETUTCDATE() for table " + tableName); SyncTracerExtended.TraceCommandAndParameters((IDbCommand)sqlCommand); sqlCommand.ExecuteNonQuery(); } connection.Close(); } catch (Exception ex) { if (connection.State == ConnectionState.Open) { connection.Close(); } Log.Error("[DistributedDb] UpdateTriggerScripts Error", ex, this); } }
/// <summary> /// This method tries to look up the service configuration from the MetadataCache. /// For the first request, there will be no item in the cache and so, an object of type /// SyncServiceConfiguration is created, initialized and added to the MetadataCache. /// </summary> private void CreateConfiguration() { Type serviceType = base.GetType(); // Check if we already have a configuration for the service in the metadata cache. MetadataCacheItem item = MetadataCache.TryLookup(serviceType); if (null == item) { SyncTracer.Info("Creating SyncServiceConfiguration for service type {0}", serviceType); item = new MetadataCacheItem(serviceType); // Initialize the private member since it will then have default values. // In case of an error in the static initializer, we can refer to the default values // of configuration. _syncConfiguration = new SyncServiceConfiguration(typeof(T)); // This will invoke the static initialization method. _syncConfiguration.Initialize(serviceType); item.Configuration = _syncConfiguration; MetadataCache.AddCacheItem(serviceType, item); SyncTracer.Info("SyncServiceConfiguration for service type {0} created successfully!", serviceType); } else { _syncConfiguration = item.Configuration; } // Invoke the testhook Initialize method. // Note: This needs to be called regardless of whether we find the configuration in the // cache or not because this is on a per request basis. _syncConfiguration.InvokeTestHookInitializeMethod(serviceType); }
/// <summary> /// Read and parse the incoming request stream for a POST request. /// </summary> private void ReadIncomingRequestStreamForPost() { if (null == _serviceHost.RequestStream || !_serviceHost.RequestStream.CanRead) { SyncTracer.Info("Request stream for HTTP POST is empty, null or cannot be read."); return; } try { var reader = WebUtil.GetSyncReader(_serviceHost.GetRequestContentSerializationFormat(), _serviceHost.RequestStream, _configuration.TypeToTableGlobalNameMapping.Keys.ToArray()); reader.Start(); while (reader.Next()) { switch (reader.ItemType) { case ReaderItemType.Entry: IOfflineEntity entity = reader.GetItem(); if (entity.ServiceMetadata.IsTombstone) { if (String.IsNullOrEmpty(entity.ServiceMetadata.Id)) { throw SyncServiceException.CreateBadRequestError(Strings.TombstoneEntityHasNoId); } WebUtil.ParseIdStringAndPopulateKeyFields(entity, _serviceHost.ServiceBaseUri); } _entityList.Add(entity); bool hasTempId = false; if (reader.HasTempId()) { // Save the entity id to tempId mapping for use later when writing response. _idToTempIdMapping.Add(WebUtil.GenerateOfflineEntityId(entity), reader.GetTempId()); hasTempId = true; } // Make sure, we have atleast one of Id or TempId if (String.IsNullOrEmpty(entity.ServiceMetadata.Id) && !hasTempId) { throw SyncServiceException.CreateBadRequestError(Strings.BothIdAndTempIdAreMissing); } break; case ReaderItemType.SyncBlob: _syncBlob = reader.GetServerBlob(); break; } } } catch (XmlException exception) { SyncTracer.Warning("XmlException: {0}", WebUtil.GetExceptionMessage(exception)); throw SyncServiceException.CreateBadRequestError(Strings.BadRequestPayload); } }