Skip to content

Commit 33558bc

Browse files
authored
Merge pull request #73 from linkedin/ngangwar/ngangwar_update_ideal_state
Update Ideal State API creating new Ideal State
2 parents 4e72b20 + 272abc9 commit 33558bc

3 files changed

Lines changed: 68 additions & 1 deletion

File tree

helix-core/src/main/java/org/apache/helix/manager/zk/ZKHelixAdmin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1734,7 +1734,7 @@ public void updateIdealState(String clusterName, String resourceName, IdealState
17341734
resourceName));
17351735
}
17361736
// Update by way of merge
1737-
ZKUtil.createOrUpdate(_zkClient, zkPath, idealState.getRecord(), true, true);
1737+
ZKUtil.upsertWithOptionalCreate(_zkClient, zkPath, idealState.getRecord(), true, true, false);
17381738
}
17391739

17401740
/**

helix-core/src/main/java/org/apache/helix/manager/zk/ZKUtil.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,11 @@ public static void createOrUpdate(String zkAddress, String path, final ZNRecord
427427

428428
public static void createOrUpdate(RealmAwareZkClient client, String path, final ZNRecord record,
429429
final boolean persistent, final boolean mergeOnUpdate) {
430+
upsertWithOptionalCreate(client, path, record, persistent, mergeOnUpdate, true);
431+
}
432+
433+
public static void upsertWithOptionalCreate(RealmAwareZkClient client, String path, final ZNRecord record,
434+
final boolean persistent, final boolean mergeOnUpdate, final boolean allowCreate) {
430435
int retryCount = 0;
431436
while (retryCount < RETRYLIMIT) {
432437
try {
@@ -443,6 +448,10 @@ public ZNRecord update(ZNRecord currentData) {
443448
};
444449
client.updateDataSerialized(path, updater);
445450
} else {
451+
if (!allowCreate) {
452+
throw new HelixException(("Path " + path + " does not exist"));
453+
}
454+
446455
CreateMode mode = (persistent) ? CreateMode.PERSISTENT : CreateMode.EPHEMERAL;
447456
client.create(path, record, mode);
448457
}

helix-core/src/test/java/org/apache/helix/manager/zk/TestZKUtil.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,62 @@ record = _gZkClient.readData(path);
218218
put("k2", "v2");
219219
}}, record.getMapField("map"));
220220
}
221+
222+
@Test()
223+
public void testUpsertWithOptionalCreate_CreateWhenNotExists() {
224+
// Test: Create node when it doesn't exist and allowCreate is true
225+
String path = PropertyPathBuilder.instanceConfig(clusterName, "id10");
226+
ZNRecord record = new ZNRecord("id10");
227+
record.setSimpleField("key1", "value1");
228+
229+
// Node doesn't exist yet
230+
AssertJUnit.assertFalse(_gZkClient.exists(path));
231+
232+
// Create with allowCreate=true, persistent=true
233+
ZKUtil.upsertWithOptionalCreate(_gZkClient, path, record, true, true, true);
234+
235+
// Verify node was created
236+
AssertJUnit.assertTrue(_gZkClient.exists(path));
237+
ZNRecord result = _gZkClient.readData(path);
238+
AssertJUnit.assertEquals("id10", result.getId());
239+
AssertJUnit.assertEquals("value1", result.getSimpleField("key1"));
240+
}
241+
242+
@Test()
243+
public void testUpsertWithOptionalCreate_NoCreateWhenNotExists() {
244+
// Test: Don't create node when it doesn't exist and allowCreate is false
245+
String path = PropertyPathBuilder.instanceConfig(clusterName, "id11");
246+
ZNRecord record = new ZNRecord("id11");
247+
record.setSimpleField("key1", "value1");
248+
249+
// Node doesn't exist yet
250+
AssertJUnit.assertFalse(_gZkClient.exists(path));
251+
252+
// Try to update with allowCreate=false
253+
ZKUtil.upsertWithOptionalCreate(_gZkClient, path, record, true, true, false);
254+
255+
// Verify node was NOT created
256+
AssertJUnit.assertFalse(_gZkClient.exists(path));
257+
}
258+
259+
@Test()
260+
public void testUpsertWithOptionalCreate_UpdateExistingWithAllowCreateFalse() {
261+
// Test: Update should work even when allowCreate=false if node exists
262+
String path = PropertyPathBuilder.instanceConfig(clusterName, "id15");
263+
264+
// Create initial node
265+
ZNRecord initial = new ZNRecord("id15");
266+
initial.setSimpleField("key1", "value1");
267+
_gZkClient.createPersistent(path, initial);
268+
269+
// Update with allowCreate=false (should still work since node exists)
270+
ZNRecord update = new ZNRecord("id15");
271+
update.setSimpleField("key2", "value2");
272+
ZKUtil.upsertWithOptionalCreate(_gZkClient, path, update, true, true, false);
273+
274+
// Verify update happened
275+
ZNRecord result = _gZkClient.readData(path);
276+
AssertJUnit.assertEquals("value1", result.getSimpleField("key1"));
277+
AssertJUnit.assertEquals("value2", result.getSimpleField("key2"));
278+
}
221279
}

0 commit comments

Comments
 (0)