|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.datastax.oss.driver.internal.core.session; |
| 19 | + |
| 20 | +import com.datastax.oss.driver.api.core.metadata.Node; |
| 21 | +import com.datastax.oss.driver.api.core.metadata.NodeState; |
| 22 | +import com.datastax.oss.driver.api.core.session.SessionLifecycleManager; |
| 23 | +import com.datastax.oss.driver.internal.core.context.InternalDriverContext; |
| 24 | +import com.datastax.oss.driver.internal.core.metadata.DefaultNode; |
| 25 | +import com.datastax.oss.driver.internal.core.metadata.NodeStateEvent; |
| 26 | +import com.datastax.oss.driver.internal.core.pool.ChannelPool; |
| 27 | +import com.datastax.oss.driver.internal.core.util.concurrent.CompletableFutures; |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.concurrent.CompletableFuture; |
| 32 | +import java.util.concurrent.CompletionStage; |
| 33 | + |
| 34 | +public class DefaultSessionLifecycleManager implements SessionLifecycleManager { |
| 35 | + private final DefaultSession session; |
| 36 | + private final InternalDriverContext context; |
| 37 | + private CompletableFuture<Void> suspendFuture; |
| 38 | + private Map<Node, NodeState> lastState; |
| 39 | + |
| 40 | + public DefaultSessionLifecycleManager(DefaultSession session) { |
| 41 | + this.session = session; |
| 42 | + this.context = (InternalDriverContext) session.getContext(); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public synchronized CompletionStage<Void> suspendAsync() { |
| 47 | + if (suspendFuture != null) { |
| 48 | + return suspendFuture; |
| 49 | + } |
| 50 | + suspendFuture = new CompletableFuture<>(); |
| 51 | + // ControlConnection would try to reconnect when it receives the event that |
| 52 | + // node was brought down; closing the very channel to this node prevents that. |
| 53 | + this.context |
| 54 | + .getControlConnection() |
| 55 | + .channel() |
| 56 | + .close() |
| 57 | + .addListener( |
| 58 | + f -> { |
| 59 | + if (f.isSuccess()) { |
| 60 | + forceNodesDown() |
| 61 | + .whenComplete( |
| 62 | + (ignored, throwable) -> { |
| 63 | + if (throwable != null) { |
| 64 | + suspendFuture.completeExceptionally(throwable); |
| 65 | + } else { |
| 66 | + suspendFuture.complete(null); |
| 67 | + } |
| 68 | + }); |
| 69 | + } else { |
| 70 | + suspendFuture.completeExceptionally(f.cause()); |
| 71 | + } |
| 72 | + }); |
| 73 | + return suspendFuture; |
| 74 | + } |
| 75 | + |
| 76 | + private CompletionStage<Void> forceNodesDown() { |
| 77 | + lastState = new HashMap<>(); |
| 78 | + ArrayList<CompletionStage<Void>> closeFutures = new ArrayList<>(); |
| 79 | + for (Map.Entry<Node, ChannelPool> e : session.getPools().entrySet()) { |
| 80 | + Node node = e.getKey(); |
| 81 | + NodeState currentState = node.getState(); |
| 82 | + lastState.put(node, currentState); |
| 83 | + closeFutures.add(e.getValue().closeFuture()); |
| 84 | + context |
| 85 | + .getEventBus() |
| 86 | + .fire(NodeStateEvent.changed(currentState, NodeState.FORCED_DOWN, (DefaultNode) node)); |
| 87 | + } |
| 88 | + return CompletableFutures.allDone(closeFutures); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public void resume() { |
| 93 | + if (suspendFuture == null) { |
| 94 | + return; |
| 95 | + } |
| 96 | + suspendFuture.whenComplete( |
| 97 | + (ignored, throwable) -> { |
| 98 | + if (throwable != null || lastState == null) { |
| 99 | + return; |
| 100 | + } |
| 101 | + synchronized (this) { |
| 102 | + for (Map.Entry<Node, NodeState> e : lastState.entrySet()) { |
| 103 | + NodeStateEvent changed = |
| 104 | + NodeStateEvent.changed( |
| 105 | + NodeState.FORCED_DOWN, e.getValue(), (DefaultNode) e.getKey()); |
| 106 | + this.context.getEventBus().fire(changed); |
| 107 | + } |
| 108 | + lastState = null; |
| 109 | + suspendFuture = null; |
| 110 | + context.getControlConnection().reconnectNow(); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public synchronized boolean isSuspended() { |
| 117 | + return suspendFuture != null; |
| 118 | + } |
| 119 | +} |
0 commit comments