1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.linecorp.centraldogma.server.command;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import com.google.common.base.MoreObjects;
23
24 import com.linecorp.centraldogma.server.management.ServerStatus;
25 import com.linecorp.centraldogma.server.management.ServerStatusManager;
26
27
28
29
30 public final class CommandExecutorStatusManager {
31
32 private static final Logger logger = LoggerFactory.getLogger(ServerStatusManager.class);
33
34 private final CommandExecutor executor;
35
36
37
38
39 public CommandExecutorStatusManager(CommandExecutor executor) {
40 this.executor = executor;
41 }
42
43
44
45
46 public CommandExecutor executor() {
47 return executor;
48 }
49
50
51
52
53 public boolean writable() {
54 return executor.isWritable();
55 }
56
57
58
59
60 public boolean replicating() {
61 return executor.isStarted();
62 }
63
64
65
66
67
68
69
70 public synchronized void updateStatus(UpdateServerStatusCommand command) {
71 final ServerStatus serverStatus = command.serverStatus();
72 updateStatus(serverStatus);
73 }
74
75
76
77
78
79
80
81 public synchronized void updateStatus(ServerStatus serverStatus) {
82 if (serverStatus.replicating()) {
83
84 setReplicating(serverStatus.replicating());
85 setWritable(serverStatus.writable());
86 } else {
87
88 setWritable(serverStatus.writable());
89 setReplicating(serverStatus.replicating());
90 }
91 }
92
93
94
95
96
97
98 public synchronized boolean setWritable(boolean newWritable) {
99 final boolean writable = writable();
100 if (writable == newWritable) {
101 return true;
102 }
103 executor.setWritable(newWritable);
104 if (newWritable) {
105 logger.warn("Left read-only mode.");
106 } else {
107 logger.warn("Entered read-only mode. replication: {}", replicating());
108 }
109 return true;
110 }
111
112
113
114
115
116
117
118
119
120
121 public synchronized boolean setReplicating(boolean newReplicating) {
122 if (newReplicating) {
123 if (replicating()) {
124 return true;
125 }
126 try {
127 logger.info("Enabling replication ...");
128 executor.start().get();
129 logger.info("Enabled replication. read-only: {}", !writable());
130 return true;
131 } catch (Exception cause) {
132 logger.warn("Failed to start the command executor:", cause);
133 return false;
134 }
135 } else {
136 if (!replicating()) {
137 return true;
138 }
139 try {
140 logger.info("Disabling replication ...");
141 executor.stop().get();
142 logger.info("Disabled replication");
143 return true;
144 } catch (Exception cause) {
145 logger.warn("Failed to stop the command executor:", cause);
146 return false;
147 }
148 }
149 }
150
151 @Override
152 public String toString() {
153 return MoreObjects.toStringHelper(this)
154 .add("isWritable", writable())
155 .add("replicating", replicating())
156 .toString();
157 }
158 }