|
| 1 | +/* |
| 2 | + * Copyright 2017 MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.async.client; |
| 18 | + |
| 19 | +import com.mongodb.session.ClientSession; |
| 20 | +import com.mongodb.ClientSessionOptions; |
| 21 | +import com.mongodb.ReadPreference; |
| 22 | +import com.mongodb.session.ServerSession; |
| 23 | +import com.mongodb.async.SingleResultCallback; |
| 24 | +import com.mongodb.binding.AsyncClusterBinding; |
| 25 | +import com.mongodb.binding.AsyncReadBinding; |
| 26 | +import com.mongodb.binding.AsyncReadWriteBinding; |
| 27 | +import com.mongodb.binding.AsyncWriteBinding; |
| 28 | +import com.mongodb.connection.ClusterConnectionMode; |
| 29 | +import com.mongodb.connection.ClusterDescription; |
| 30 | +import com.mongodb.connection.Server; |
| 31 | +import com.mongodb.connection.ServerDescription; |
| 32 | +import com.mongodb.diagnostics.logging.Logger; |
| 33 | +import com.mongodb.diagnostics.logging.Loggers; |
| 34 | +import com.mongodb.internal.session.ClientSessionImpl; |
| 35 | +import com.mongodb.operation.AsyncOperationExecutor; |
| 36 | +import com.mongodb.operation.AsyncReadOperation; |
| 37 | +import com.mongodb.operation.AsyncWriteOperation; |
| 38 | +import com.mongodb.selector.ServerSelector; |
| 39 | + |
| 40 | +import java.util.List; |
| 41 | + |
| 42 | +import static com.mongodb.assertions.Assertions.isTrue; |
| 43 | +import static com.mongodb.assertions.Assertions.notNull; |
| 44 | +import static com.mongodb.internal.async.ErrorHandlingResultCallback.errorHandlingCallback; |
| 45 | + |
| 46 | +class AsyncOperationExecutorImpl implements AsyncOperationExecutor { |
| 47 | + private static final Logger LOGGER = Loggers.getLogger("client"); |
| 48 | + private final MongoClientImpl mongoClient; |
| 49 | + |
| 50 | + AsyncOperationExecutorImpl(final MongoClientImpl mongoClient) { |
| 51 | + this.mongoClient = mongoClient; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public <T> void execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, |
| 56 | + final SingleResultCallback<T> callback) { |
| 57 | + execute(operation, readPreference, null, callback); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public <T> void execute(final AsyncReadOperation<T> operation, final ReadPreference readPreference, final ClientSession session, |
| 62 | + final SingleResultCallback<T> callback) { |
| 63 | + notNull("operation", operation); |
| 64 | + notNull("readPreference", readPreference); |
| 65 | + notNull("callback", callback); |
| 66 | + final SingleResultCallback<T> errHandlingCallback = errorHandlingCallback(callback, LOGGER); |
| 67 | + getClientSession(session, new SingleResultCallback<ClientSession>(){ |
| 68 | + @Override |
| 69 | + public void onResult(final ClientSession clientSession, final Throwable t) { |
| 70 | + if (t != null) { |
| 71 | + errHandlingCallback.onResult(null, t); |
| 72 | + } else { |
| 73 | + final AsyncReadBinding binding = getReadWriteBinding(readPreference, clientSession, |
| 74 | + session == null && clientSession != null); |
| 75 | + operation.executeAsync(binding, new SingleResultCallback<T>() { |
| 76 | + @Override |
| 77 | + public void onResult(final T result, final Throwable t) { |
| 78 | + try { |
| 79 | + errHandlingCallback.onResult(result, t); |
| 80 | + } finally { |
| 81 | + binding.release(); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public <T> void execute(final AsyncWriteOperation<T> operation, final SingleResultCallback<T> callback) { |
| 92 | + execute(operation, null, callback); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public <T> void execute(final AsyncWriteOperation<T> operation, final ClientSession session, |
| 97 | + final SingleResultCallback<T> callback) { |
| 98 | + notNull("operation", operation); |
| 99 | + notNull("callback", callback); |
| 100 | + final SingleResultCallback<T> errHandlingCallback = errorHandlingCallback(callback, LOGGER); |
| 101 | + getClientSession(session, new SingleResultCallback<ClientSession>() { |
| 102 | + @Override |
| 103 | + public void onResult(final ClientSession clientSession, final Throwable t) { |
| 104 | + if (t != null) { |
| 105 | + errHandlingCallback.onResult(null, t); |
| 106 | + } else { |
| 107 | + final AsyncWriteBinding binding = getReadWriteBinding(ReadPreference.primary(), clientSession, |
| 108 | + session == null && clientSession != null); |
| 109 | + operation.executeAsync(binding, new SingleResultCallback<T>() { |
| 110 | + @Override |
| 111 | + public void onResult(final T result, final Throwable t) { |
| 112 | + try { |
| 113 | + errHandlingCallback.onResult(result, t); |
| 114 | + } finally { |
| 115 | + binding.release(); |
| 116 | + } |
| 117 | + } |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + private void getClientSession(final ClientSession clientSessionFromOperation, |
| 125 | + final SingleResultCallback<ClientSession> callback) { |
| 126 | + if (clientSessionFromOperation != null) { |
| 127 | + isTrue("ClientSession from same MongoClient", |
| 128 | + clientSessionFromOperation.getOriginator() == mongoClient); |
| 129 | + callback.onResult(clientSessionFromOperation, null); |
| 130 | + } else { |
| 131 | + createClientSession(ClientSessionOptions.builder().causallyConsistent(false).build(), callback); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private AsyncReadWriteBinding getReadWriteBinding(final ReadPreference readPreference, final ClientSession session, |
| 136 | + final boolean ownsSession) { |
| 137 | + notNull("readPreference", readPreference); |
| 138 | + AsyncReadWriteBinding readWriteBinding = new AsyncClusterBinding(mongoClient.getCluster(), readPreference); |
| 139 | + if (session != null) { |
| 140 | + readWriteBinding = new ClientSessionBinding(session, ownsSession, readWriteBinding); |
| 141 | + } |
| 142 | + return readWriteBinding; |
| 143 | + } |
| 144 | + |
| 145 | + @SuppressWarnings("deprecation") |
| 146 | + private void createClientSession(final ClientSessionOptions options, final SingleResultCallback<ClientSession> callback) { |
| 147 | + if (mongoClient.getSettings().getCredentialList().size() > 1) { |
| 148 | + callback.onResult(null, null); |
| 149 | + } else { |
| 150 | + ClusterDescription clusterDescription = mongoClient.getCluster().getDescription(); |
| 151 | + if (!getServerDescriptionListToConsiderForSessionSupport(clusterDescription).isEmpty() |
| 152 | + && clusterDescription.getLogicalSessionTimeoutMinutes() != null) { |
| 153 | + getServerSessionCallback(options, callback).onResult(mongoClient.getServerSessionPool().get(), null); |
| 154 | + } else { |
| 155 | + mongoClient.getCluster().selectServerAsync(new ServerSelector() { |
| 156 | + @Override |
| 157 | + public List<ServerDescription> select(final ClusterDescription clusterDescription) { |
| 158 | + return getServerDescriptionListToConsiderForSessionSupport(clusterDescription); |
| 159 | + } |
| 160 | + }, new SingleResultCallback<Server>() { |
| 161 | + @Override |
| 162 | + public void onResult(final Server server, final Throwable t) { |
| 163 | + if (t != null) { |
| 164 | + callback.onResult(null, null); |
| 165 | + } else if (server.getDescription().getLogicalSessionTimeoutMinutes() == null) { |
| 166 | + callback.onResult(null, null); |
| 167 | + } else { |
| 168 | + getServerSessionCallback(options, callback).onResult(mongoClient.getServerSessionPool().get(), null); |
| 169 | + } |
| 170 | + } |
| 171 | + }); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @SuppressWarnings("deprecation") |
| 177 | + private List<ServerDescription> getServerDescriptionListToConsiderForSessionSupport(final ClusterDescription clusterDescription) { |
| 178 | + if (clusterDescription.getConnectionMode() == ClusterConnectionMode.SINGLE) { |
| 179 | + return clusterDescription.getAny(); |
| 180 | + } else { |
| 181 | + return clusterDescription.getAnyPrimaryOrSecondary(); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + private SingleResultCallback<ServerSession> getServerSessionCallback(final ClientSessionOptions options, |
| 186 | + final SingleResultCallback<ClientSession> callback) { |
| 187 | + return new SingleResultCallback<ServerSession>() { |
| 188 | + @Override |
| 189 | + public void onResult(final ServerSession serverSession, final Throwable t) { |
| 190 | + if (t != null) { |
| 191 | + callback.onResult(null, t); |
| 192 | + } else { |
| 193 | + callback.onResult(new ClientSessionImpl(mongoClient.getServerSessionPool(), serverSession, |
| 194 | + System.identityHashCode(mongoClient), options), null); |
| 195 | + } |
| 196 | + } |
| 197 | + }; |
| 198 | + } |
| 199 | +} |
0 commit comments