You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2019/07/23 11:04:33 UTC
[tinkerpop] 01/02: Merge branch 'tp33' into tp34
This is an automated email from the ASF dual-hosted git repository.
spmallette pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tinkerpop.git
commit 73fd48615ef76f4ab73354a07595ddd500b7d836
Merge: 0f95e00 b355d87
Author: Stephen Mallette <sp...@genoprime.com>
AuthorDate: Tue Jul 23 07:04:02 2019 -0400
Merge branch 'tp33' into tp34
CHANGELOG.asciidoc | 1 +
docs/src/dev/provider/index.asciidoc | 22 +++-----
docs/src/reference/the-traversal.asciidoc | 14 +----
docs/src/upgrade/release-3.3.x.asciidoc | 32 ++++++++++++
.../AbstractRemoteTraversalSideEffects.java | 4 ++
.../remote/traversal/EmbeddedRemoteTraversal.java | 4 ++
.../EmbeddedRemoteTraversalSideEffects.java | 4 ++
.../process/remote/traversal/RemoteTraversal.java | 3 ++
.../traversal/RemoteTraversalSideEffects.java | 4 ++
.../gremlin/process/traversal/Traversal.java | 8 ++-
.../src/Gremlin.Net/Driver/Connection.cs | 4 +-
.../Driver/Remote/DriverRemoteTraversal.cs | 3 ++
.../Remote/DriverRemoteTraversalSideEffects.cs | 2 +
.../Driver/ResultsAggregation/AggregatorFactory.cs | 2 +
.../Driver/SingleMessageResultReceiver.cs | 2 +
gremlin-dotnet/src/Gremlin.Net/Driver/Tokens.cs | 9 ++++
.../Process/Traversal/DefaultTraversal.cs | 3 ++
.../Gremlin.Net/Process/Traversal/ITraversal.cs | 2 +
.../Process/Traversal/ITraversalSideEffects.cs | 1 +
gremlin-dotnet/src/Gremlin.Net/Structure/Graph.cs | 2 +-
.../tinkerpop/gremlin/driver/ResultQueue.java | 3 ++
.../apache/tinkerpop/gremlin/driver/Tokens.java | 61 +++++++++++++++++++++-
.../driver/remote/DriverRemoteTraversal.java | 6 +++
.../remote/DriverRemoteTraversalSideEffects.java | 4 ++
.../server/op/traversal/TraversalOpProcessor.java | 20 +++++++
pom.xml | 3 +-
26 files changed, 190 insertions(+), 33 deletions(-)
diff --cc gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
index b9845c7,f1a61a4..a8a3f77
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
@@@ -74,85 -58,77 +74,85 @@@ namespace Gremlin.Net.Drive
public async Task ConnectAsync()
{
await _webSocketConnection.ConnectAsync(_uri).ConfigureAwait(false);
+ BeginReceiving();
}
- public async Task CloseAsync()
+ public int NrRequestsInFlight => _callbackByRequestId.Count;
+
+ public bool IsOpen => _webSocketConnection.IsOpen && Volatile.Read(ref _connectionState) != Closed;
+
+ public Task<ResultSet<T>> SubmitAsync<T>(RequestMessage requestMessage)
{
- await _webSocketConnection.CloseAsync().ConfigureAwait(false);
+ var receiver = new ResponseHandlerForSingleRequestMessage<T>(_graphSONReader);
+ _callbackByRequestId.GetOrAdd(requestMessage.RequestId, receiver);
+ _writeQueue.Enqueue(requestMessage);
+ BeginSendingMessages();
+ return receiver.Result;
}
- public bool IsOpen => _webSocketConnection.IsOpen;
-
- private async Task SendAsync(RequestMessage message)
+ private void BeginReceiving()
{
- var graphsonMsg = _graphSONWriter.WriteObject(message);
- var serializedMsg = _messageSerializer.SerializeMessage(graphsonMsg);
- await _webSocketConnection.SendMessageAsync(serializedMsg).ConfigureAwait(false);
+ var state = Volatile.Read(ref _connectionState);
+ if (state == Closed) return;
+ ReceiveMessagesAsync().Forget();
}
- private async Task<IReadOnlyCollection<T>> ReceiveAsync<T>()
+ private async Task ReceiveMessagesAsync()
{
- ResponseStatus status;
- IAggregator aggregator = null;
- var isAggregatingSideEffects = false;
- var result = new List<T>();
- do
+ while (true)
{
- var received = await _webSocketConnection.ReceiveMessageAsync().ConfigureAwait(false);
- var receivedMsg = _messageSerializer.DeserializeMessage<ResponseMessage<JToken>>(received);
-
- status = receivedMsg.Status;
- status.ThrowIfStatusIndicatesError();
-
- if (status.Code == ResponseStatusCode.Authenticate)
+ try
+ {
+ var received = await _webSocketConnection.ReceiveMessageAsync().ConfigureAwait(false);
+ Parse(received);
+ }
+ catch (Exception e)
{
- await AuthenticateAsync().ConfigureAwait(false);
+ await CloseConnectionBecauseOfFailureAsync(e).ConfigureAwait(false);
+ break;
}
- else if (status.Code != ResponseStatusCode.NoContent)
+ }
+ }
+
+ private void Parse(byte[] received)
+ {
+ var receivedMsg = _messageSerializer.DeserializeMessage<ResponseMessage<JToken>>(received);
-
++
+ try
+ {
+ TryParseResponseMessage(receivedMsg);
+ }
+ catch (Exception e)
+ {
+ if (_callbackByRequestId.TryRemove(receivedMsg.RequestId, out var responseHandler))
{
- var receivedData = typeof(T) == typeof(JToken)
- ? new[] { receivedMsg.Result.Data }
- : _graphSONReader.ToObject(receivedMsg.Result.Data);
-
- foreach (var d in receivedData)
- #pragma warning disable 612,618
- if (receivedMsg.Result.Meta.ContainsKey(Tokens.ArgsSideEffectKey))
- {
- if (aggregator == null)
- aggregator =
- new AggregatorFactory().GetAggregatorFor(
- (string) receivedMsg.Result.Meta[Tokens.ArgsAggregateTo]);
- aggregator.Add(d);
- isAggregatingSideEffects = true;
- }
- else
- {
- result.Add(d);
- }
- #pragma warning disable 612,618
+ responseHandler.HandleFailure(e);
}
- } while (status.Code == ResponseStatusCode.PartialContent || status.Code == ResponseStatusCode.Authenticate);
+ }
+ }
- if (isAggregatingSideEffects)
- return new List<T> {(T) aggregator.GetAggregatedResult()};
- return result;
+ private void TryParseResponseMessage(ResponseMessage<JToken> receivedMsg)
+ {
+ var status = receivedMsg.Status;
+ status.ThrowIfStatusIndicatesError();
+
+ if (status.Code == ResponseStatusCode.Authenticate)
+ {
+ Authenticate();
+ return;
+ }
+
+ if (status.Code != ResponseStatusCode.NoContent)
+ _callbackByRequestId[receivedMsg.RequestId].HandleReceived(receivedMsg);
+
+ if (status.Code == ResponseStatusCode.Success || status.Code == ResponseStatusCode.NoContent)
+ {
+ _callbackByRequestId[receivedMsg.RequestId].Finalize(status.Attributes);
+ _callbackByRequestId.TryRemove(receivedMsg.RequestId, out _);
+ }
}
- private async Task AuthenticateAsync()
+ private void Authenticate()
{
if (string.IsNullOrEmpty(_username) || string.IsNullOrEmpty(_password))
throw new InvalidOperationException(
@@@ -172,73 -147,6 +172,73 @@@
return Convert.ToBase64String(authBytes);
}
+ private void BeginSendingMessages()
+ {
+ if (Interlocked.CompareExchange(ref _writeInProgress, 1, 0) != 0)
+ return;
+ SendMessagesFromQueueAsync().Forget();
+ }
+
+ private async Task SendMessagesFromQueueAsync()
+ {
+ while (_writeQueue.TryDequeue(out var msg))
+ {
+ try
+ {
+ await SendMessageAsync(msg).ConfigureAwait(false);
+ }
+ catch (Exception e)
+ {
+ await CloseConnectionBecauseOfFailureAsync(e).ConfigureAwait(false);
+ break;
+ }
+ }
+ Interlocked.CompareExchange(ref _writeInProgress, 0, 1);
+
+ // Since the loop ended and the write in progress was set to 0
+ // a new item could have been added, write queue can contain items at this time
+ if (!_writeQueue.IsEmpty && Interlocked.CompareExchange(ref _writeInProgress, 1, 0) == 0)
+ {
+ await SendMessagesFromQueueAsync().ConfigureAwait(false);
+ }
+ }
+
+ private async Task CloseConnectionBecauseOfFailureAsync(Exception exception)
+ {
+ EmptyWriteQueue();
+ await CloseAsync().ConfigureAwait(false);
+ NotifyAboutConnectionFailure(exception);
+ }
+
+ private void EmptyWriteQueue()
+ {
+ while (_writeQueue.TryDequeue(out _))
+ {
+ }
+ }
-
++
+ private void NotifyAboutConnectionFailure(Exception exception)
+ {
+ foreach (var cb in _callbackByRequestId.Values)
+ {
+ cb.HandleFailure(exception);
+ }
+ _callbackByRequestId.Clear();
+ }
+
+ private async Task SendMessageAsync(RequestMessage message)
+ {
+ var graphsonMsg = _graphSONWriter.WriteObject(message);
+ var serializedMsg = _messageSerializer.SerializeMessage(graphsonMsg);
+ await _webSocketConnection.SendMessageAsync(serializedMsg).ConfigureAwait(false);
+ }
+
+ public async Task CloseAsync()
+ {
+ Interlocked.Exchange(ref _connectionState, Closed);
+ await _webSocketConnection.CloseAsync().ConfigureAwait(false);
+ }
+
#region IDisposable Support
private bool _disposed;
diff --cc gremlin-dotnet/src/Gremlin.Net/Driver/SingleMessageResultReceiver.cs
index 1e211cd,0000000..7b66e61
mode 100644,000000..100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/SingleMessageResultReceiver.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/SingleMessageResultReceiver.cs
@@@ -1,88 -1,0 +1,90 @@@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Threading.Tasks;
+using Gremlin.Net.Driver.Messages;
+using Gremlin.Net.Driver.ResultsAggregation;
+using Gremlin.Net.Structure.IO.GraphSON;
+using Newtonsoft.Json.Linq;
+
+namespace Gremlin.Net.Driver
+{
+ internal class ResponseHandlerForSingleRequestMessage<T> : IResponseHandlerForSingleRequestMessage
+ {
+ public Task<ResultSet<T>> Result => _tcs.Task;
+
+ private readonly TaskCompletionSource<ResultSet<T>> _tcs =
+ new TaskCompletionSource<ResultSet<T>>(TaskCreationOptions.RunContinuationsAsynchronously);
+
+ private readonly GraphSONReader _graphSONReader;
+ private bool _isAggregatingSideEffects;
+ private IAggregator _aggregator;
+ private readonly List<T> _result = new List<T>();
+
+ public ResponseHandlerForSingleRequestMessage(GraphSONReader graphSonReader)
+ {
+ _graphSONReader = graphSonReader;
+ }
+
+ public void HandleReceived(ResponseMessage<JToken> received)
+ {
+ var receivedData = typeof(T) == typeof(JToken)
+ ? new[] {received.Result.Data}
+ : _graphSONReader.ToObject(received.Result.Data);
++ #pragma warning disable 612,618
+ foreach (var d in receivedData)
+ {
+ if (received.Result.Meta.ContainsKey(Tokens.ArgsSideEffectKey))
+ {
+ if (_aggregator == null)
+ _aggregator =
+ new AggregatorFactory().GetAggregatorFor(
+ (string) received.Result.Meta[Tokens.ArgsAggregateTo]);
+ _aggregator.Add(d);
+ _isAggregatingSideEffects = true;
+ }
+ else
+ {
+ _result.Add(d);
+ }
+ }
++ #pragma warning disable 612,618
+ }
+
+ public void Finalize(Dictionary<string, object> statusAttributes)
+ {
+ var resultSet =
+ new ResultSet<T>(
+ _isAggregatingSideEffects ? new List<T> {(T) _aggregator.GetAggregatedResult()} : _result,
+ statusAttributes);
+ _tcs.TrySetResult(resultSet);
+ }
+
+ public void HandleFailure(Exception objException)
+ {
+ _tcs.TrySetException(objException);
+ }
+ }
+}
diff --cc gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
index 7190acf,81487ca..16bacf6
--- a/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
+++ b/gremlin-driver/src/main/java/org/apache/tinkerpop/gremlin/driver/Tokens.java
@@@ -30,11 -30,22 +30,23 @@@ public final class Tokens
public static final String OPS_BYTECODE = "bytecode";
public static final String OPS_EVAL = "eval";
public static final String OPS_INVALID = "invalid";
+ public static final String OPS_CLOSE = "close";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String OPS_GATHER = "gather";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String OPS_KEYS = "keys";
- public static final String OPS_CLOSE = "close";
+ public static final String REQUEST_ID = "requestId";
public static final String ARGS_BATCH_SIZE = "batchSize";
public static final String ARGS_BINDINGS = "bindings";
public static final String ARGS_ALIASES = "aliases";
@@@ -47,14 -58,67 +59,61 @@@
public static final String ARGS_MANAGE_TRANSACTION = "manageTransaction";
public static final String ARGS_SASL = "sasl";
public static final String ARGS_SASL_MECHANISM = "saslMechanism";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String ARGS_SIDE_EFFECT = "sideEffect";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String ARGS_AGGREGATE_TO = "aggregateTo";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String ARGS_SIDE_EFFECT_KEY = "sideEffectKey";
+ /**
- * @deprecated As of release 3.1.0-incubating, replaced by {@link #ARGS_ALIASES}.
- */
- @Deprecated
- public static final String ARGS_REBINDINGS = "rebindings";
-
- /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String VAL_AGGREGATE_TO_BULKSET = "bulkset";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String VAL_AGGREGATE_TO_LIST = "list";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String VAL_AGGREGATE_TO_MAP = "map";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String VAL_AGGREGATE_TO_NONE = "none";
+
+ /**
+ * @deprecated As of release 3.3.8, not directly replaced in the protocol as side-effect retrieval after
+ * traversal iteration is not being promoted anymore as a feature.
+ */
+ @Deprecated
public static final String VAL_AGGREGATE_TO_SET = "set";
public static final String VAL_TRAVERSAL_SOURCE_ALIAS = "g";