/// <summary> /// Apply changes sent by a client to the server. /// </summary> /// <param name="serverBlob">Blob sent in the incoming request</param> /// <param name="entities">Changes from the client</param> /// <returns>Response containing the new knowledge and conflict/error information.</returns> public ApplyChangesResponse ApplyChanges(byte[] serverBlob, List<IOfflineEntity> entities) { WebUtil.CheckArgumentNull(serverBlob, "serverBlob"); WebUtil.CheckArgumentNull(entities, "entities"); if (0 == serverBlob.Length) { throw new InvalidOperationException("serverBlob is empty"); } var syncBlob = new SyncBlob(); SyncBlob incomingBlob = SyncBlob.DeSerialize(serverBlob); PopulateClientScopeNameAndSyncId(incomingBlob); // Set the scope name in the response blob. syncBlob.ClientScopeName = incomingBlob.ClientScopeName; // If the requested scope does not exists, then throw an error since we // don't initialize scopes on upload requests. if (!CheckIfScopeExists()) { throw SyncServiceException.CreateResourceNotFound("Scope does not exist"); } byte[] clientKnowledgeBlob = incomingBlob.ClientKnowledge; // Initialize a SqlSyncProvider object. _sqlSyncProvider = CreateSqlSyncProviderInstance(_clientScopeName, _serverConnectionString, _configuration.SyncObjectSchema); var response = new ApplyChangesResponse(); // Deserialize the knowledge or create new empty knowledge. SyncKnowledge clientKnowledge = GetSyncKnowledgeFromBlob(clientKnowledgeBlob); // If there are no entities to upload, then return the client knowledge as is. if (entities.Count == 0) { response.Conflicts = new List<SyncConflict>(); response.Errors = new List<SyncError>(); syncBlob.ClientKnowledge = clientKnowledge.Serialize(); response.ServerBlob = syncBlob.Serialize(); return response; } // Client never has any forgotten knowledge. So create a new one. var forgottenKnowledge = new ForgottenKnowledge(_sqlSyncProvider.IdFormats, clientKnowledge); // Convert the entities to dataset using the custom converter. DataSet changesDS = _converter.ConvertEntitiesToDataSet(entities); var stats = new SyncSessionStatistics(); var sessionContext = new SyncSessionContext(_sqlSyncProvider.IdFormats, new SyncCallbacks()); _sqlSyncProvider.BeginSession(SyncProviderPosition.Remote, sessionContext); ulong tickCount = 0; SyncKnowledge updatedClientKnowldege; try { uint batchSize; SyncKnowledge serverKnowledge; // This gives us the server knowledge. _sqlSyncProvider.GetSyncBatchParameters(out batchSize, out serverKnowledge); var changeBatch = new ChangeBatch(_sqlSyncProvider.IdFormats, clientKnowledge, forgottenKnowledge); changeBatch.SetLastBatch(); //Note: There is a possiblity of (-ve) item exceptions , between two uploads from the // same client (for example: in case of RI failures). This would result in an incorrect value if the function // FindMinTickCountForReplica is used to get the last tickcount. So, we need to ignore the -ve item exceptions // when finding the tickcount for the client replica from the server knowledge. /* Logic: * SyncKnowledge.GetKnowledgeForItemId could be used for itemid Zero and then we can find the mintickcount for client replica id. * This does not however seem to work, so we use the KnowledgeInspector and enumerate over each ClockVector * and find the client clockvector and get its tickcount. * * Assumption: The above approach assumes that we don't have any positive exceptions in the knowledge. */ try { // Check if the client replica key exists. uint clientReplicaKey = serverKnowledge.ReplicaKeyMap.LookupReplicaKey(_clientSyncId); var ki = new KnowledgeInspector(1, serverKnowledge); var clockVector = (ClockVector)ki.ScopeClockVector; int noOfReplicaKeys = clockVector.Count; for (int i = noOfReplicaKeys - 1; i >= 0; i--) { if (clockVector[i].ReplicaKey == clientReplicaKey) { tickCount = clockVector[i].TickCount; break; } } } catch (ReplicaNotFoundException exception) { SyncTracer.Info("ReplicaNotFoundException. NEW CLIENT. Exception details: {0}", WebUtil.GetExceptionMessage(exception)); // If the knowedge does not contain the client replica (first apply), initialize tickcount to zero. tickCount = 0; } // Increment the tickcount tickCount++; // update the made with knowledge to include the new tickcount. updatedClientKnowldege = new SyncKnowledge(_sqlSyncProvider.IdFormats, _clientSyncId, tickCount); updatedClientKnowldege.Combine(clientKnowledge); // The incoming data does not have metadata for each item, so we need to create it at this point. AddSyncColumnsToDataSet(changesDS, tickCount); // Make DbSyncContext var dbSyncContext = new DbSyncContext { IsDataBatched = false, IsLastBatch = true, DataSet = changesDS, MadeWithKnowledge = updatedClientKnowldege, MadeWithForgottenKnowledge = forgottenKnowledge, ScopeProgress = new DbSyncScopeProgress() }; _conflicts = new List<SyncConflict>(); _syncErrors = new List<SyncError>(); // Subscribe to the ApplyChangeFailed event to handle conflicts. _sqlSyncProvider.ApplyChangeFailed += SqlSyncProviderApplyChangeFailed; // Subscribe to the ChangesApplied event to read the server tickcount incase there are any conflicts. _sqlSyncProvider.ChangesApplied += SqlSyncProviderChangesApplied; //NOTE: The ConflictResolutionPolicy pass into the method is IGNORED. // Conflicts can be logged by subscribing to the failed events _sqlSyncProvider.ProcessChangeBatch(Microsoft.Synchronization.ConflictResolutionPolicy.DestinationWins, changeBatch, dbSyncContext, new SyncCallbacks(), stats); if (0 != _conflicts.Count) { _sqlSyncProvider.GetSyncBatchParameters(out batchSize, out serverKnowledge); // The way the current P2P provider works, versions are bumped up when conflicts are resolved on the server. // This would result in us sending the changes to the client on the next download request. We want // to not enumerate that change again on the next request from the same client. // The solution is to get the server knowledge after all changes are applied and then // project the knowledge of each conflictign item and add it as a positive exception to the updated client knowledge. AddConflictItemsKnowledgeToClientKnowledge(updatedClientKnowldege, serverKnowledge); } } finally { _sqlSyncProvider.EndSession(sessionContext); } // Don't send any updates to the server knowledge since the client has not got any updates yet. // This updated knowledge will only include an update to the client tickcount. // The client would obtain the server knowledge when it does a get changes. // If we include the serverknowlege, the client would never get any items that are // between the current server knowledge and the client known server knowledge. syncBlob.ClientKnowledge = updatedClientKnowldege.Serialize(); response.ServerBlob = syncBlob.Serialize(); response.Conflicts = _conflicts; response.Errors = _syncErrors; return response; }
public SyncSessionStatistics ApplyChanges(ConflictResolutionPolicy resolutionPolicy, ChangeBatch sourceChanges, object changeData) { Log("ProcessChangeBatch: {0}", this.peerProvider.Connection.ConnectionString); DbSyncContext dataRetriever = changeData as DbSyncContext; if (dataRetriever != null && dataRetriever.IsDataBatched) { string remotePeerId = dataRetriever.MadeWithKnowledge.ReplicaId.ToString(); //Data is batched. The client should have uploaded this file to us prior to calling ApplyChanges. //So look for it. //The Id would be the DbSyncContext.BatchFileName which is just the batch file name without the complete path string localBatchFileName = null; if (!this.batchIdToFileMapper.TryGetValue(dataRetriever.BatchFileName, out localBatchFileName)) { //Service has not received this file. Throw exception throw new FaultException <WebSyncFaultException>(new WebSyncFaultException("No batch file uploaded for id " + dataRetriever.BatchFileName, null)); } dataRetriever.BatchFileName = localBatchFileName; } SyncSessionStatistics sessionStatistics = new SyncSessionStatistics(); this.peerProvider.ProcessChangeBatch(resolutionPolicy, sourceChanges, changeData, new SyncCallbacks(), sessionStatistics); return(sessionStatistics); }
public override void ProcessChangeBatch(ConflictResolutionPolicy resolutionPolicy, ChangeBatch sourceChanges, object changeDataRetriever, SyncCallbacks syncCallbacks, SyncSessionStatistics sessionStatistics) { DbSyncContext context = changeDataRetriever as DbSyncContext; // Check if the data is batched if (context != null && context.IsDataBatched) { string filename = new FileInfo(context.BatchFileName).Name; // Retrieve the remote id from the MadeWithKnowledge.ReplicaId. MadeWithKnowledge is the local knowledge of the peer // that is enumerating the changes string remoteId = context.MadeWithKnowledge.ReplicaId.ToString(); // Check if service already has this file if (!Proxy.HasUploadedBatchFile(filename, remoteId)) { // Upload this file to remote service byte[] content = null; using (Stream stream = new FileStream(context.BatchFileName, FileMode.Open, FileAccess.Read)) { content = new byte[stream.Length]; stream.Read(content, 0, content.Length); } if (content != null) { Proxy.UploadBatchFile(filename, content, remoteId); } } context.BatchFileName = filename; } SyncSessionStatistics stats = Proxy.ApplyChanges(resolutionPolicy, sourceChanges, changeDataRetriever); sessionStatistics.ChangesApplied += stats.ChangesApplied; sessionStatistics.ChangesFailed += stats.ChangesFailed; }
public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destinationKnowledge, out object changeDataRetriever) { GetChangesParameters changesWrapper = proxy.GetChanges(batchSize, destinationKnowledge); //Retrieve the ChangeDataRetriever and the ChangeBatch changeDataRetriever = changesWrapper.DataRetriever; DbSyncContext context = changeDataRetriever as DbSyncContext; //Check to see if the data is batched. if (context != null && context.IsDataBatched) { if (this.localBatchingDirectory == null) { //Retrieve the remote peer id from the MadeWithKnowledge.ReplicaId. MadeWithKnowledge is the local knowledge of the peer //that is enumerating the changes. string remotePeerId = context.MadeWithKnowledge.ReplicaId.ToString(); //Generate a unique Id for the directory. //We use the peer id of the store enumerating the changes so that the local temp directory is same for a given source //across sync sessions. This enables us to restart a failed sync by not downloading already received files. string sessionDir = Path.Combine(this.batchingDirectory, "WebSync_" + remotePeerId); this.localBatchingDirectory = new DirectoryInfo(sessionDir); //Create the directory if it doesnt exist. if (!this.localBatchingDirectory.Exists) { this.localBatchingDirectory.Create(); } } string localFileName = Path.Combine(this.localBatchingDirectory.FullName, context.BatchFileName); FileInfo localFileInfo = new FileInfo(localFileName); //Download the file only if doesnt exist if (!localFileInfo.Exists) { byte[] remoteFileContents = this.proxy.DownloadBatchFile(context.BatchFileName); using (FileStream localFileStream = new FileStream(localFileName, FileMode.Create, FileAccess.Write)) { localFileStream.Write(remoteFileContents, 0, remoteFileContents.Length); } } //Set DbSyncContext.Batchfile name to the new local file name context.BatchFileName = localFileName; } return(changesWrapper.ChangeBatch); }
private void DownloadBatchingFile(DbSyncContext context) { string localFilename = Path.Combine(LocalBatchingDirectory.FullName, context.BatchFileName); FileInfo localFileInfo = new FileInfo(localFilename); // Download the file if not yet exist if (!localFileInfo.Exists) { byte[] remoteFileContents = Proxy.DownloadBatchFile(context.BatchFileName); using (Stream localFileStream = new FileStream(localFilename, FileMode.Create, FileAccess.Write)) { localFileStream.Write(remoteFileContents, 0, remoteFileContents.Length); } } // Set the batch file name of the context to the new local file name context.BatchFileName = localFilename; }
public GetChangesParameters GetChanges(uint batchSize, SyncKnowledge destinationKnowledge) { GetChangesParameters changesWrapper = new GetChangesParameters(); changesWrapper.ChangeBatch = this.peerProvider.GetChangeBatch(batchSize, destinationKnowledge, out changesWrapper.DataRetriever); DbSyncContext context = changesWrapper.DataRetriever as DbSyncContext; //Check to see if data is batched if (context != null && context.IsDataBatched) { string fileName = new FileInfo(context.BatchFileName).Name; this.batchIdToFileMapper[fileName] = context.BatchFileName; context.BatchFileName = fileName; } return(changesWrapper); }
public override ChangeBatch GetChangeBatch(uint batchSize, SyncKnowledge destKnowledge, out object changeDataRetriever) { DbChangesParameters changesParams = Proxy.GetChanges(batchSize, destKnowledge); changeDataRetriever = changesParams.DataRetriever; DbSyncContext context = changeDataRetriever as DbSyncContext; // Check if the data is batched if (context != null && context.IsDataBatched) { CheckAndCreateBatchingDirectory(context); DownloadBatchingFile(context); } return(changesParams.ChangeBatch); }
private void CheckAndCreateBatchingDirectory(DbSyncContext context) { if (LocalBatchingDirectory == null) { // Retrieve the remote id from MadeWithKnowledge.ReplicaId. MadeWithKnowledge is the local knowledge of the peer // that is enumerating the changes string remoteId = context.MadeWithKnowledge.ReplicaId.ToString(); // Generate a unique id for the directory. We use the remote id of the store enumerating the changes so that the local temp directory // is same for a gicen source across sync sessions. This enables us to restart a failed sync by not downloading already received files string batchDir = Path.Combine(BatchingDirectory, "WebSync_" + remoteId); LocalBatchingDirectory = new DirectoryInfo(batchDir); // Create the directory if not yet exist if (!LocalBatchingDirectory.Exists) { LocalBatchingDirectory.Create(); } } }
public DbChangesParameters GetChanges(uint batchSize, Microsoft.Synchronization.SyncKnowledge destKnowledge) { DbChangesParameters changesParams = new DbChangesParameters(); changesParams.ChangeBatch = Provider.GetChangeBatch(batchSize, destKnowledge, out changesParams.DataRetriever); DbSyncContext context = changesParams.DataRetriever as DbSyncContext; // Check to see if data is batched if (context != null && context.IsDataBatched) { // Don't send the file location info, just send the file string filename = new FileInfo(context.BatchFileName).Name; batchIdToFileMapper[filename] = context.BatchFileName; context.BatchFileName = filename; } return(changesParams); }
public GetChangesParameters GetChanges(uint batchSize, SyncKnowledge destinationKnowledge) { Log("GetChangeBatch: {0}", this.peerProvider.Connection.ConnectionString); GetChangesParameters changesWrapper = new GetChangesParameters(); changesWrapper.ChangeBatch = this.peerProvider.GetChangeBatch(batchSize, destinationKnowledge, out changesWrapper.DataRetriever); DbSyncContext context = changesWrapper.DataRetriever as DbSyncContext; //Check to see if data is batched if (context != null && context.IsDataBatched) { Log("GetChangeBatch: Data Batched. Current Batch #:{0}", ++this.batchCount); //Dont send the file location info. Just send the file name string fileName = new FileInfo(context.BatchFileName).Name; this.batchIdToFileMapper[fileName] = context.BatchFileName; context.BatchFileName = fileName; } return(changesWrapper); }
public SyncSessionStatistics ApplyChanges(ConflictResolutionPolicy resolutionPolicy, ChangeBatch sourceChanges, object changeData) { DbSyncContext context = changeData as DbSyncContext; // Check to see if data is batched if (context != null && context.IsDataBatched) { string remoteId = context.MadeWithKnowledge.ReplicaId.ToString(); // Data is batched. The client should have uploaded this file to us prior to calling ApplyChanges, so look for it // The id would be the DbSyncContext.BatchFileName which is just the batch filename without the complete path string localBatchFilename = null; if (!batchIdToFileMapper.TryGetValue(context.BatchFileName, out localBatchFilename)) // Service did not received this file { throw new FaultException <WebSyncFaultException>(new WebSyncFaultException(string.Format("No batch file uploaded for the id {0}.", context.BatchFileName), null)); } context.BatchFileName = localBatchFilename; } SyncSessionStatistics sessionStatistics = new SyncSessionStatistics(); Provider.ProcessChangeBatch(resolutionPolicy, sourceChanges, changeData, new SyncCallbacks(), sessionStatistics); return(sessionStatistics); }