-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
AdminApiSchemaAutoUpdateTest.java
285 lines (244 loc) · 12.1 KB
/
AdminApiSchemaAutoUpdateTest.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
/**
* 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 com.google.common.collect.Sets;
import java.lang.reflect.Field;
import lombok.extern.slf4j.Slf4j;
import org.apache.avro.reflect.AvroAlias;
import org.apache.avro.reflect.AvroDefault;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.broker.service.Topic;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.SchemaAutoUpdateCompatibilityStrategy;
import org.apache.pulsar.common.policies.data.SchemaCompatibilityStrategy;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
@Slf4j
@Test(groups = "broker-admin")
public class AdminApiSchemaAutoUpdateTest extends MockedPulsarServiceBaseTest {
@BeforeMethod
@Override
public void setup() throws Exception {
super.internalSetup();
// Setup namespaces
admin.clusters().createCluster("test", ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress()).build());
TenantInfoImpl tenantInfo = new TenantInfoImpl(Sets.newHashSet("role1", "role2"), Sets.newHashSet("test"));
admin.tenants().createTenant("prop-xyz", tenantInfo);
admin.namespaces().createNamespace("prop-xyz/ns1", Sets.newHashSet("test"));
admin.namespaces().createNamespace("prop-xyz/test/ns1");
admin.namespaces().createNamespace("prop-xyz/ns2", Sets.newHashSet("test"));
admin.namespaces().createNamespace("prop-xyz/test/ns2");
}
@AfterMethod(alwaysRun = true)
@Override
public void cleanup() throws Exception {
super.internalCleanup();
}
private void testAutoUpdateBackward(String namespace, String topicName) throws Exception {
Assert.assertEquals(admin.namespaces().getSchemaAutoUpdateCompatibilityStrategy(namespace), null);
admin.namespaces().setSchemaAutoUpdateCompatibilityStrategy(namespace,
SchemaAutoUpdateCompatibilityStrategy.Backward);
try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
p.send(new V1Data("test1", 1));
}
log.info("try with forward compat, should fail");
try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
Assert.fail("Forward compat schema should be rejected");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with backward compat, should succeed");
try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
p.send(new V2Data("test2"));
}
}
private void testAutoUpdateForward(String namespace, String topicName) throws Exception {
Assert.assertEquals(admin.namespaces().getSchemaAutoUpdateCompatibilityStrategy(namespace), null);
admin.namespaces().setSchemaAutoUpdateCompatibilityStrategy(namespace,
SchemaAutoUpdateCompatibilityStrategy.Forward);
try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
p.send(new V1Data("test1", 1));
}
log.info("try with backward compat, should fail");
try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
Assert.fail("Backward compat schema should be rejected");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with forward compat, should succeed");
try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
p.send(new V3Data("test2", 1, 2));
}
}
private void testAutoUpdateFull(String namespace, String topicName) throws Exception {
Assert.assertEquals(admin.namespaces().getSchemaAutoUpdateCompatibilityStrategy(namespace), null);
try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
p.send(new V1Data("test1", 1));
}
log.info("try with backward compat only, should fail");
try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
Assert.fail("Backward compat only schema should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with forward compat only, should fail");
try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
Assert.fail("Forward compat only schema should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with fully compat");
try (Producer<V4Data> p = pulsarClient.newProducer(Schema.AVRO(V4Data.class)).topic(topicName).create()) {
p.send(new V4Data("test2", 1, (short)100));
}
}
private void testAutoUpdateDisabled(String namespace, String topicName) throws Exception {
Assert.assertEquals(admin.namespaces().getSchemaAutoUpdateCompatibilityStrategy(namespace), null);
admin.namespaces().setSchemaAutoUpdateCompatibilityStrategy(namespace,
SchemaAutoUpdateCompatibilityStrategy.AutoUpdateDisabled);
try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
p.send(new V1Data("test1", 1));
}
log.info("try with backward compat only, should fail");
try (Producer<V2Data> p = pulsarClient.newProducer(Schema.AVRO(V2Data.class)).topic(topicName).create()) {
Assert.fail("Backward compat only schema should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with forward compat only, should fail");
try (Producer<V3Data> p = pulsarClient.newProducer(Schema.AVRO(V3Data.class)).topic(topicName).create()) {
Assert.fail("Forward compat only schema should fail");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("try with fully compat, should fail");
try (Producer<V4Data> p = pulsarClient.newProducer(Schema.AVRO(V4Data.class)).topic(topicName).create()) {
Assert.fail("Fully compat schema should fail, autoupdate disabled");
} catch (PulsarClientException e) {
Assert.assertTrue(e.getMessage().contains("IncompatibleSchemaException"));
}
log.info("Should still be able to connect with original schema");
try (Producer<V1Data> p = pulsarClient.newProducer(Schema.AVRO(V1Data.class)).topic(topicName).create()) {
p.send(new V1Data("test2", 2));
}
admin.namespaces().setSchemaAutoUpdateCompatibilityStrategy(namespace,
SchemaAutoUpdateCompatibilityStrategy.Full);
for (int i = 0; i < 100; i++) {
Topic t = pulsar.getBrokerService().getTopicIfExists(topicName).get().get();
// get around fact that field is private and topic can be persistent or non-persistent
Field strategy = t.getClass().getSuperclass().getDeclaredField("schemaCompatibilityStrategy");
strategy.setAccessible(true);
if (strategy.get(t) == SchemaCompatibilityStrategy.FULL) {
break;
}
}
log.info("try with fully compat, again");
try (Producer<V4Data> p = pulsarClient.newProducer(Schema.AVRO(V4Data.class)).topic(topicName).create()) {
p.send(new V4Data("test2", 1, (short)100));
}
}
@AvroAlias(space="blah", alias="data")
static class V1Data {
String foo;
int bar;
V1Data(String foo, int bar) {
this.foo = foo;
this.bar = bar;
}
}
// backward compatible with V1Data
@AvroAlias(space="blah", alias="data")
static class V2Data {
String foo;
V2Data(String foo) {
this.foo = foo;
}
}
// forward compatible with V1Data
@AvroAlias(space="blah", alias="data")
static class V3Data {
String foo;
int bar;
long baz;
V3Data(String foo, int bar, long baz) {
this.foo = foo;
this.bar = bar;
this.baz = baz;
}
}
// fully compatible with V1Data
@AvroAlias(space="blah", alias="data")
static class V4Data {
String foo;
int bar;
@AvroDefault(value = "10")
short blah;
V4Data(String foo, int bar, short blah) {
this.foo = foo;
this.bar = bar;
this.blah = blah;
}
}
@Test
public void testBackwardV2() throws Exception {
testAutoUpdateBackward("prop-xyz/ns1", "persistent://prop-xyz/ns1/backward");
testAutoUpdateBackward("prop-xyz/ns2", "non-persistent://prop-xyz/ns2/backward-np");
}
@Test
public void testForwardV2() throws Exception {
testAutoUpdateForward("prop-xyz/ns1", "persistent://prop-xyz/ns1/forward");
testAutoUpdateForward("prop-xyz/ns2", "non-persistent://prop-xyz/ns2/forward-np");
}
@Test
public void testFullV2() throws Exception {
testAutoUpdateFull("prop-xyz/ns1", "persistent://prop-xyz/ns1/full");
testAutoUpdateFull("prop-xyz/ns2", "non-persistent://prop-xyz/ns2/full-np");
}
@Test
public void testDisabledV2() throws Exception {
testAutoUpdateDisabled("prop-xyz/ns1", "persistent://prop-xyz/ns1/disabled");
testAutoUpdateDisabled("prop-xyz/ns2", "non-persistent://prop-xyz/ns2/disabled-np");
}
@Test
public void testBackwardV1() throws Exception {
testAutoUpdateBackward("prop-xyz/test/ns1", "persistent://prop-xyz/test/ns1/backward");
testAutoUpdateBackward("prop-xyz/test/ns2", "non-persistent://prop-xyz/test/ns2/backward-np");
}
@Test
public void testForwardV1() throws Exception {
testAutoUpdateForward("prop-xyz/test/ns1", "persistent://prop-xyz/test/ns1/forward");
testAutoUpdateForward("prop-xyz/test/ns2", "non-persistent://prop-xyz/test/ns2/forward-np");
}
@Test
public void testFullV1() throws Exception {
testAutoUpdateFull("prop-xyz/test/ns1", "persistent://prop-xyz/test/ns1/full");
testAutoUpdateFull("prop-xyz/test/ns2", "non-persistent://prop-xyz/test/ns2/full-np");
}
@Test
public void testDisabledV1() throws Exception {
testAutoUpdateDisabled("prop-xyz/test/ns1", "persistent://prop-xyz/test/ns1/disabled");
testAutoUpdateDisabled("prop-xyz/test/ns2", "non-persistent://prop-xyz/test/ns2/disabled-np");
}
}