/// <summary> /// Gets the chunk the specified id belongs in. /// </summary> /// <param name="id">The id to search for.</param> /// <returns>The chunk the specified id belongs in.</returns> private DatabaseChunk GetChunk(ObjectId id) { var value = _chunks.SingleOrDefault(e => ChunkMarker.IsBetween(e.Start, e.End, id.ToString())); if (value == null) { _lock.ExitReadLock(); throw new ChunkMovedException(); } return(value); }
/// <summary> /// Processes an update operation. /// </summary> /// <param name="dataOperation">The document that represents the operation.</param> /// <param name="message">The original message.</param> private void ProcessUpdateOperation(Document dataOperation, Message message) { UpdateOperation updateOperation = new UpdateOperation(dataOperation["update"].ValueAsDocument); if (!updateOperation.Valid) { SendMessage(new Message(message, updateOperation.ErrorMessage, false)); return; } _chunkListLock.EnterReadLock(); var chunk = _chunkList.SingleOrDefault(e => ChunkMarker.IsBetween(e.Start, e.End, updateOperation.DocumentId.ToString())); _chunkListLock.ExitReadLock(); NodeDefinition node = chunk == null ? null : chunk.Node; if (node == null) { SendMessage(new Message(message, new DataOperationResult(ErrorCodes.FailedMessage, "No storage node up for the specified id range."), false)); return; } Message operationMessage = new Message(node, message.Data, true); operationMessage.SetResponseCallback(delegate(Message originalOperationMessage) { if (originalOperationMessage.Success) { DataOperationResult result = (DataOperationResult)originalOperationMessage.Response.Data; Document resultDocument = new Document(result.Result); if (!resultDocument["success"].ValueAsBoolean && (ErrorCodes)Enum.Parse(typeof(ErrorCodes), resultDocument["errorcode"].ValueAsString) == ErrorCodes.ChunkMoved) { ProcessUpdateOperation(dataOperation, message); return; } SendMessage(new Message(message, result, false)); return; } SendMessage(new Message(message, new DataOperationResult(ErrorCodes.FailedMessage, "Failed to send message to storage node."), false)); }); SendMessage(operationMessage); }