-
Notifications
You must be signed in to change notification settings - Fork 612
/
SiteStatementTest.java
225 lines (193 loc) · 11.2 KB
/
SiteStatementTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package org.infinispan.cli.interpreter;
import org.infinispan.Cache;
import org.infinispan.cli.interpreter.result.ResultKeys;
import org.infinispan.commons.api.BasicCacheContainer;
import org.infinispan.configuration.cache.CacheMode;
import org.infinispan.configuration.cache.ConfigurationBuilder;
import org.infinispan.context.Flag;
import org.infinispan.manager.DefaultCacheManager;
import org.infinispan.statetransfer.CommitManager;
import org.infinispan.test.CacheManagerCallable;
import org.infinispan.xsite.AbstractTwoSitesTest;
import org.infinispan.xsite.XSiteAdminOperations;
import org.infinispan.xsite.statetransfer.XSiteStateProvider;
import org.infinispan.xsite.statetransfer.XSiteStateTransferManager;
import org.testng.annotations.Test;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import static java.lang.String.format;
import static org.infinispan.test.TestingUtil.extractComponent;
import static org.infinispan.test.TestingUtil.withCacheManager;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.fail;
/**
* @author Tristan Tarrant
* @since 5.2
*/
@Test(groups = "xsite", testName = "cli.interpreter.SiteStatementTest")
public class SiteStatementTest extends AbstractTwoSitesTest {
public SiteStatementTest() {
implicitBackupCache = true;
}
@Override
protected ConfigurationBuilder getNycActiveConfig() {
return getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC, true);
}
@Override
protected ConfigurationBuilder getLonActiveConfig() {
return getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC, true);
}
public void testSiteStatus() throws Exception {
Interpreter lonInterpreter = interpreter("LON", 0);
String lonCache = cache("LON", 0).getName();
String lonSessionId = lonInterpreter.createSessionId(lonCache);
Interpreter nycInterpreter = interpreter("NYC", 0);
String nycCache = cache("NYC", 0).getName();
String nycSessionId = nycInterpreter.createSessionId(nycCache);
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status NYC;", "online");
assertInterpreterOutput(nycInterpreter, nycSessionId, format("site --status %s.LON;", lonCache), "online");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --offline NYC;", "ok");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --online NYC;", "ok");
}
public void testSiteStateTransfer() throws Exception {
Interpreter lonInterpreter = interpreter("LON", 0);
String lonCache = cache("LON", 0).getName();
String lonSessionId = lonInterpreter.createSessionId(lonCache);
Interpreter nycInterpreter = interpreter("NYC", 0);
String nycCache = cache("NYC", 0).getName();
String nycSessionId = nycInterpreter.createSessionId(nycCache);
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --offline NYC;", "ok");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status NYC;", "offline");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --push NYC;", "ok");
assertEventuallyNoStateTransferInReceivingSite("NYC", nycCache, 10, TimeUnit.SECONDS);
assertEventuallyNoStateTransferInSendingSite("LON", lonCache, 10, TimeUnit.SECONDS);
assertInterpreterOutput(nycInterpreter, nycSessionId, "site --sendingsite;", "null");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --pushstatus;", "NYC=OK");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --clearpushstatus;", "ok");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --pushstatus;", (String) null);
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --cancelpush NYC;", "ok");
assertInterpreterOutput(nycInterpreter, nycSessionId, "site --cancelreceive LON;", "ok");
}
public void testSiteWithoutBackups() throws Exception {
final String cacheName = "no-backups";
withCacheManager(new CacheManagerCallable(new DefaultCacheManager()) {
@Override
public void call() {
ConfigurationBuilder builder = getDefaultClusteredCacheConfig(CacheMode.LOCAL);
builder.sites().disableBackups(true);
cm.defineConfiguration(cacheName, builder.build());
Cache cache = cm.getCache(cacheName);
Interpreter interpreter = cache.getAdvancedCache().getComponentRegistry().getComponent(Interpreter.class);
String sessionId = interpreter.createSessionId(cacheName);
try {
assertInterpreterError(interpreter, sessionId, "site --status;",
"ISPN019033: The cache '" + cacheName + "' has no backups configured.");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});
}
public void testContainerOperations() throws Exception {
site(LON).cacheManagers().forEach(cacheManager -> cacheManager.defineConfiguration("another-cache", lonConfigurationBuilder().build()));
site(LON).cacheManagers().forEach(cacheManager -> cacheManager.defineConfiguration("another-cache-2", getDefaultClusteredCacheConfig(CacheMode.DIST_SYNC).build()));
site(LON).waitForClusterToForm("another-cache");
site(LON).waitForClusterToForm("another-cache-2");
Interpreter lonInterpreter = interpreter(LON, 0);
String lonCache = cache(LON, 0).getName();
String lonSessionId = lonInterpreter.createSessionId(lonCache);
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --offlineall NYC;", (output, error) -> {
assertEquals(null, error);
String outFormat = "%s: %s";
if (!output.contains(format(outFormat, BasicCacheContainer.DEFAULT_CACHE_NAME, XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", BasicCacheContainer.DEFAULT_CACHE_NAME, output));
}
if (!output.contains(format(outFormat, "another-cache", XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", "another-cache", output));
}
if (output.contains("another-cache-2")) {
fail(format("Cache '%s' should not be present in the output: %s", "another-cache-2", output));
}
});
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status NYC;", "offline");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status \"another-cache\".NYC;", "offline");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --onlineall NYC;", (output, error) -> {
assertEquals(null, error);
String outFormat = "%s: %s";
if (!output.contains(format(outFormat, BasicCacheContainer.DEFAULT_CACHE_NAME, XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", BasicCacheContainer.DEFAULT_CACHE_NAME, output));
}
if (!output.contains(format(outFormat, "another-cache", XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", "another-cache", output));
}
if (output.contains("another-cache-2")) {
fail(format("Cache '%s' should not be present in the output: %s", "another-cache-2", output));
}
});
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status NYC;", "online");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --status \"another-cache\".NYC;", "online");
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --pushall NYC;", (output, error) -> {
assertEquals(null, error);
String outFormat = "%s: %s";
if (!output.contains(format(outFormat, BasicCacheContainer.DEFAULT_CACHE_NAME, XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", BasicCacheContainer.DEFAULT_CACHE_NAME, output));
}
if (!output.contains(format(outFormat, "another-cache", XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", "another-cache", output));
}
if (output.contains("another-cache-2")) {
fail(format("Cache '%s' should not be present in the output: %s", "another-cache-2", output));
}
});
assertInterpreterOutput(lonInterpreter, lonSessionId, "site --cancelpushall NYC;", (output, error) -> {
assertEquals(null, error);
String outFormat = "%s: %s";
if (!output.contains(format(outFormat, BasicCacheContainer.DEFAULT_CACHE_NAME, XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", BasicCacheContainer.DEFAULT_CACHE_NAME, output));
}
if (!output.contains(format(outFormat, "another-cache", XSiteAdminOperations.SUCCESS))) {
fail(format("Cache '%s' should be present in the output: %s", "another-cache", output));
}
if (output.contains("another-cache-2")) {
fail(format("Cache '%s' should not be present in the output: %s", "another-cache-2", output));
}
});
}
private void assertInterpreterOutput(Interpreter interpreter, String sessionId, String command, String expected) throws Exception {
assertInterpreterOutput(interpreter, sessionId, command, (output, error) -> {
assertEquals(null, error);
assertEquals(expected, output);
});
}
private void assertInterpreterOutput(Interpreter interpreter, String sessionId, String command, OutputValidator validator) throws Exception {
Objects.requireNonNull(validator);
Map<String, String> result = interpreter.execute(sessionId, command);
validator.validate(result.get(ResultKeys.OUTPUT.toString()), result.get(ResultKeys.ERROR.toString()));
}
private void assertInterpreterError(Interpreter interpreter, String sessionId, String command, String expected) throws Exception {
assertInterpreterOutput(interpreter, sessionId, command, (output, error) -> {
assertEquals(null, output);
assertEquals(expected, error);
});
}
private Interpreter interpreter(String site, int cache) {
return cache(site, cache).getAdvancedCache().getComponentRegistry().getComponent(Interpreter.class);
}
private void assertEventuallyNoStateTransferInReceivingSite(String siteName, String cacheName, long timeout, TimeUnit unit) {
assertEventuallyInSite(siteName, cacheName, cache -> {
CommitManager commitManager = extractComponent(cache, CommitManager.class);
return !commitManager.isTracking(Flag.PUT_FOR_STATE_TRANSFER) &&
!commitManager.isTracking(Flag.PUT_FOR_X_SITE_STATE_TRANSFER) &&
commitManager.isEmpty();
}, timeout, unit);
}
private void assertEventuallyNoStateTransferInSendingSite(String siteName, String cacheName, long timeout, TimeUnit unit) {
assertEventuallyInSite(siteName, cacheName, cache ->
extractComponent(cache, XSiteStateProvider.class).getCurrentStateSending().isEmpty() &&
extractComponent(cache, XSiteStateTransferManager.class).getRunningStateTransfers().isEmpty(), timeout, unit);
}
private interface OutputValidator {
void validate(String output, String error);
}
}