/
AdminApiOffloadTest.java
359 lines (314 loc) · 16.8 KB
/
AdminApiOffloadTest.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/**
* 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.
*/
package org.apache.pulsar.broker.admin;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import com.google.common.collect.Sets;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.apache.bookkeeper.mledger.LedgerOffloader;
import org.apache.bookkeeper.mledger.ManagedLedgerInfo;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.broker.service.persistent.PersistentTopic;
import org.apache.pulsar.client.admin.LongRunningProcessStatus;
import org.apache.pulsar.client.admin.PulsarAdminException.ConflictException;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.naming.NamespaceName;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.OffloadPolicies;
import org.apache.pulsar.common.policies.data.TenantInfo;
import org.awaitility.Awaitility;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Test(groups = "broker")
public class AdminApiOffloadTest extends MockedPulsarServiceBaseTest {
private final String testTenant = "prop-xyz";
private final String testNamespace = "ns1";
private final String myNamespace = testTenant + "/" + testNamespace;
private final String testTopic = "persistent://" + myNamespace + "/test-";
@BeforeMethod
@Override
public void setup() throws Exception {
conf.setManagedLedgerMaxEntriesPerLedger(10);
conf.setManagedLedgerMinLedgerRolloverTimeMinutes(0);
conf.setSystemTopicEnabled(true);
conf.setTopicLevelPoliciesEnabled(true);
super.internalSetup();
// Setup namespaces
admin.clusters().createCluster("test", new ClusterData(pulsar.getWebServiceAddress()));
TenantInfo tenantInfo = new TenantInfo(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test"));
admin.tenants().createTenant(testTenant, tenantInfo);
admin.namespaces().createNamespace(myNamespace, Sets.newHashSet("test"));
}
@AfterMethod(alwaysRun = true)
@Override
public void cleanup() throws Exception {
super.internalCleanup();
}
private void testOffload(String topicName, String mlName) throws Exception {
LedgerOffloader offloader = mock(LedgerOffloader.class);
when(offloader.getOffloadDriverName()).thenReturn("mock");
doReturn(offloader).when(pulsar).getManagedLedgerOffloader(any(), any());
CompletableFuture<Void> promise = new CompletableFuture<>();
doReturn(promise).when(offloader).offload(any(), any(), any());
MessageId currentId = MessageId.latest;
try (Producer<byte[]> p = pulsarClient.newProducer().topic(topicName).enableBatching(false).create()) {
for (int i = 0; i < 15; i++) {
currentId = p.send("Foobar".getBytes());
}
}
ManagedLedgerInfo info = pulsar.getManagedLedgerFactory().getManagedLedgerInfo(mlName);
assertEquals(info.ledgers.size(), 2);
assertEquals(admin.topics().offloadStatus(topicName).status,
LongRunningProcessStatus.Status.NOT_RUN);
admin.topics().triggerOffload(topicName, currentId);
assertEquals(admin.topics().offloadStatus(topicName).status,
LongRunningProcessStatus.Status.RUNNING);
try {
admin.topics().triggerOffload(topicName, currentId);
Assert.fail("Should have failed");
} catch (ConflictException e) {
// expected
}
// fail first time
promise.completeExceptionally(new Exception("Some random failure"));
assertEquals(admin.topics().offloadStatus(topicName).status,
LongRunningProcessStatus.Status.ERROR);
Assert.assertTrue(admin.topics().offloadStatus(topicName).lastError.contains("Some random failure"));
// Try again
doReturn(CompletableFuture.completedFuture(null))
.when(offloader).offload(any(), any(), any());
admin.topics().triggerOffload(topicName, currentId);
Awaitility.await().untilAsserted(() ->
assertEquals(admin.topics().offloadStatus(topicName).status,
LongRunningProcessStatus.Status.SUCCESS));
MessageIdImpl firstUnoffloaded = admin.topics().offloadStatus(topicName).firstUnoffloadedMessage;
// First unoffloaded is the first entry of current ledger
assertEquals(firstUnoffloaded.getLedgerId(), info.ledgers.get(1).ledgerId);
assertEquals(firstUnoffloaded.getEntryId(), 0);
verify(offloader, times(2)).offload(any(), any(), any());
}
@Test
public void testOffloadV2() throws Exception {
String topicName = "persistent://prop-xyz/ns1/topic1";
String mlName = "prop-xyz/ns1/persistent/topic1";
testOffload(topicName, mlName);
}
@Test
public void testOffloadV1() throws Exception {
String topicName = "persistent://prop-xyz/test/ns1/topic2";
String mlName = "prop-xyz/test/ns1/persistent/topic2";
testOffload(topicName, mlName);
}
@Test
public void testOffloadPolicies() throws Exception {
String namespaceName = "prop-xyz/ns1";
String driver = "aws-s3";
String region = "test-region";
String bucket = "test-bucket";
String endpoint = "test-endpoint";
long offloadThresholdInBytes = 0;
long offloadDeletionLagInMillis = 100L;
OffloadPolicies.OffloadedReadPriority priority = OffloadPolicies.OffloadedReadPriority.TIERED_STORAGE_FIRST;
OffloadPolicies offload1 = OffloadPolicies.create(
driver, region, bucket, endpoint, null, null,
100, 100, offloadThresholdInBytes, offloadDeletionLagInMillis, priority);
admin.namespaces().setOffloadPolicies(namespaceName, offload1);
OffloadPolicies offload2 = admin.namespaces().getOffloadPolicies(namespaceName);
assertEquals(offload1, offload2);
admin.namespaces().removeOffloadPolicies(namespaceName);
OffloadPolicies offload3 = admin.namespaces().getOffloadPolicies(namespaceName);
assertNull(offload3);
}
@Test
public void testOffloadPoliciesApi() throws Exception {
final String topicName = testTopic + UUID.randomUUID().toString();
admin.topics().createPartitionedTopic(topicName, 3);
pulsarClient.newProducer().topic(topicName).create().close();
Awaitility.await()
.until(() -> pulsar.getTopicPoliciesService().cacheIsInitialized(TopicName.get(topicName)));
OffloadPolicies offloadPolicies = admin.topics().getOffloadPolicies(topicName);
assertNull(offloadPolicies);
OffloadPolicies offload = new OffloadPolicies();
String path = "fileSystemPath";
offload.setFileSystemProfilePath(path);
admin.topics().setOffloadPolicies(topicName, offload);
Awaitility.await().untilAsserted(()
-> assertNotNull(admin.topics().getOffloadPolicies(topicName)));
assertEquals(admin.topics().getOffloadPolicies(topicName), offload);
assertEquals(admin.topics().getOffloadPolicies(topicName).getFileSystemProfilePath(), path);
admin.topics().removeOffloadPolicies(topicName);
Awaitility.await().untilAsserted(()
-> assertNull(admin.topics().getOffloadPolicies(topicName)));
assertNull(admin.topics().getOffloadPolicies(topicName));
}
@Test
public void testOffloadPoliciesAppliedApi() throws Exception {
final String topicName = testTopic + UUID.randomUUID().toString();
admin.topics().createPartitionedTopic(topicName, 3);
pulsarClient.newProducer().topic(topicName).create().close();
Awaitility.await()
.until(() -> pulsar.getTopicPoliciesService().cacheIsInitialized(TopicName.get(topicName)));
OffloadPolicies offloadPolicies = admin.topics().getOffloadPolicies(topicName, true);
OffloadPolicies brokerPolicies = OffloadPolicies
.mergeConfiguration(null,null, pulsar.getConfiguration().getProperties());
assertEquals(offloadPolicies, brokerPolicies);
//Since off loader is not really set, avoid code exceptions
LedgerOffloader topicOffloaded = mock(LedgerOffloader.class);
when(topicOffloaded.getOffloadDriverName()).thenReturn("mock");
doReturn(topicOffloaded).when(pulsar).createManagedLedgerOffloader(any());
OffloadPolicies namespacePolicies = new OffloadPolicies();
namespacePolicies.setManagedLedgerOffloadThresholdInBytes(100L);
namespacePolicies.setManagedLedgerOffloadDeletionLagInMillis(200L);
namespacePolicies.setManagedLedgerOffloadDriver("s3");
namespacePolicies.setManagedLedgerOffloadBucket("buck");
admin.namespaces().setOffloadPolicies(myNamespace, namespacePolicies);
Awaitility.await().untilAsserted(() ->
assertEquals(admin.namespaces().getOffloadPolicies(myNamespace), namespacePolicies));
assertEquals(
admin.topics().getOffloadPolicies(topicName, true), namespacePolicies);
OffloadPolicies topicPolicies = new OffloadPolicies();
topicPolicies.setManagedLedgerOffloadThresholdInBytes(200L);
topicPolicies.setManagedLedgerOffloadDeletionLagInMillis(400L);
topicPolicies.setManagedLedgerOffloadDriver("s3");
topicPolicies.setManagedLedgerOffloadBucket("buck2");
admin.topics().setOffloadPolicies(topicName, topicPolicies);
Awaitility.await().untilAsserted(()
-> assertEquals(admin.topics().getOffloadPolicies(topicName, true), topicPolicies));
admin.topics().removeOffloadPolicies(topicName);
Awaitility.await().untilAsserted(()
-> assertEquals(admin.topics().getOffloadPolicies(topicName, true), namespacePolicies));
admin.namespaces().removeOffloadPolicies(myNamespace);
Awaitility.await().untilAsserted(()
-> assertEquals(admin.topics().getOffloadPolicies(topicName, true), brokerPolicies));
}
@Test
public void testTopicLevelOffloadPartitioned() throws Exception {
testOffload(true);
}
@Test
public void testTopicLevelOffloadNonPartitioned() throws Exception {
testOffload(false);
}
private void testOffload(boolean isPartitioned) throws Exception {
String topicName = testTopic + UUID.randomUUID().toString();
int partitionNum = 3;
//1 create topic
if (isPartitioned) {
admin.topics().createPartitionedTopic(topicName, partitionNum);
} else {
admin.topics().createNonPartitionedTopic(topicName);
}
pulsarClient.newProducer().topic(topicName).enableBatching(false).create().close();
Awaitility.await()
.until(()-> pulsar.getTopicPoliciesService().cacheIsInitialized(TopicName.get(topicName)));
//2 namespace level policy should use NullLedgerOffloader by default
if (isPartitioned) {
for (int i = 0; i < partitionNum; i++) {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopicIfExists(TopicName.get(topicName).getPartition(i).toString()).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "NullLedgerOffloader");
}
} else {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopic(topicName, false).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "NullLedgerOffloader");
}
//3 construct a topic level offloadPolicies
OffloadPolicies offloadPolicies = new OffloadPolicies();
offloadPolicies.setOffloadersDirectory(".");
offloadPolicies.setManagedLedgerOffloadDriver("mock");
offloadPolicies.setManagedLedgerOffloadPrefetchRounds(10);
offloadPolicies.setManagedLedgerOffloadThresholdInBytes(1024L);
LedgerOffloader topicOffloader = mock(LedgerOffloader.class);
when(topicOffloader.getOffloadDriverName()).thenReturn("mock");
doReturn(topicOffloader).when(pulsar).createManagedLedgerOffloader(any());
Awaitility.await()
.until(() -> pulsar.getTopicPoliciesService().cacheIsInitialized(TopicName.get(topicName)));
//4 set topic level offload policies
admin.topics().setOffloadPolicies(topicName, offloadPolicies);
Awaitility.await().untilAsserted(()
-> assertNotNull(admin.topics().getOffloadPolicies(topicName)));
//5 name of offload should become "mock"
if (isPartitioned) {
for (int i = 0; i < partitionNum; i++) {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopic(TopicName.get(topicName).getPartition(i).toString(), false).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "mock");
}
} else {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopic(topicName, false).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "mock");
}
//6 remove topic level offload policy, offloader should become namespaceOffloader
LedgerOffloader namespaceOffloader = mock(LedgerOffloader.class);
when(namespaceOffloader.getOffloadDriverName()).thenReturn("s3");
Map<NamespaceName, LedgerOffloader> map = new HashMap<>();
map.put(TopicName.get(topicName).getNamespaceObject(), namespaceOffloader);
doReturn(map).when(pulsar).getLedgerOffloaderMap();
admin.topics().removeOffloadPolicies(topicName);
Awaitility.await().untilAsserted(()
-> assertNull(admin.topics().getOffloadPolicies(topicName)));
// topic level offloader should be closed
if (isPartitioned) {
verify(topicOffloader, times(partitionNum)).close();
} else {
verify(topicOffloader).close();
}
if (isPartitioned) {
for (int i = 0; i < partitionNum; i++) {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopicIfExists(TopicName.get(topicName).getPartition(i).toString()).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "s3");
}
} else {
PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService()
.getTopic(topicName, false).get().get();
assertNotNull(topic.getManagedLedger().getConfig().getLedgerOffloader());
assertEquals(topic.getManagedLedger().getConfig().getLedgerOffloader().getOffloadDriverName()
, "s3");
}
}
}