Skip to content

Commit 214553d

Browse files
virajjasaniwchevreuil
authored andcommitted
[HBASE-22591] : RecoverableZooKeeper improvement for getData, getChil… (#310)
HBASE-22591 RecoverableZooKeeper improvement for getData, getChildren, exists and removal of unused reference Signed-off-by: Wellington Chevreuil <wchevreuil@apache.org>
1 parent 5f2699e commit 214553d

File tree

1 file changed

+42
-103
lines changed

1 file changed

+42
-103
lines changed

hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/RecoverableZooKeeper.java

Lines changed: 42 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import org.apache.hadoop.hbase.trace.TraceUtil;
2828
import org.apache.hadoop.hbase.util.Bytes;
29-
import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
3029
import org.apache.hadoop.hbase.util.RetryCounter;
3130
import org.apache.hadoop.hbase.util.RetryCounterFactory;
3231
import org.apache.htrace.core.TraceScope;
@@ -169,7 +168,6 @@ public void delete(String path, int version) throws InterruptedException, Keeper
169168
boolean isRetry = false; // False for first attempt, true for all retries.
170169
while (true) {
171170
try {
172-
long startTime = EnvironmentEdgeManager.currentTime();
173171
checkZk().delete(path, version);
174172
return;
175173
} catch (KeeperException e) {
@@ -205,12 +203,21 @@ public void delete(String path, int version) throws InterruptedException, Keeper
205203
* @return A Stat instance
206204
*/
207205
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException {
206+
return exists(path, watcher, null);
207+
}
208+
209+
private Stat exists(String path, Watcher watcher, Boolean watch)
210+
throws InterruptedException, KeeperException {
208211
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.exists")) {
209212
RetryCounter retryCounter = retryCounterFactory.create();
210213
while (true) {
211214
try {
212-
long startTime = EnvironmentEdgeManager.currentTime();
213-
Stat nodeStat = checkZk().exists(path, watcher);
215+
Stat nodeStat;
216+
if (watch == null) {
217+
nodeStat = checkZk().exists(path, watcher);
218+
} else {
219+
nodeStat = checkZk().exists(path, watch);
220+
}
214221
return nodeStat;
215222
} catch (KeeperException e) {
216223
switch (e.code()) {
@@ -235,29 +242,7 @@ public Stat exists(String path, Watcher watcher) throws KeeperException, Interru
235242
* @return A Stat instance
236243
*/
237244
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException {
238-
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.exists")) {
239-
RetryCounter retryCounter = retryCounterFactory.create();
240-
while (true) {
241-
try {
242-
long startTime = EnvironmentEdgeManager.currentTime();
243-
Stat nodeStat = checkZk().exists(path, watch);
244-
return nodeStat;
245-
} catch (KeeperException e) {
246-
switch (e.code()) {
247-
case CONNECTIONLOSS:
248-
retryOrThrow(retryCounter, e, "exists");
249-
break;
250-
case OPERATIONTIMEOUT:
251-
retryOrThrow(retryCounter, e, "exists");
252-
break;
253-
254-
default:
255-
throw e;
256-
}
257-
}
258-
retryCounter.sleepUntilNextRetry();
259-
}
260-
}
245+
return exists(path, null, watch);
261246
}
262247

263248
private void retryOrThrow(RetryCounter retryCounter, KeeperException e,
@@ -277,12 +262,21 @@ private void retryOrThrow(RetryCounter retryCounter, KeeperException e,
277262
*/
278263
public List<String> getChildren(String path, Watcher watcher)
279264
throws KeeperException, InterruptedException {
265+
return getChildren(path, watcher, null);
266+
}
267+
268+
private List<String> getChildren(String path, Watcher watcher, Boolean watch)
269+
throws InterruptedException, KeeperException {
280270
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.getChildren")) {
281271
RetryCounter retryCounter = retryCounterFactory.create();
282272
while (true) {
283273
try {
284-
long startTime = EnvironmentEdgeManager.currentTime();
285-
List<String> children = checkZk().getChildren(path, watcher);
274+
List<String> children;
275+
if (watch == null) {
276+
children = checkZk().getChildren(path, watcher);
277+
} else {
278+
children = checkZk().getChildren(path, watch);
279+
}
286280
return children;
287281
} catch (KeeperException e) {
288282
switch (e.code()) {
@@ -308,29 +302,7 @@ public List<String> getChildren(String path, Watcher watcher)
308302
*/
309303
public List<String> getChildren(String path, boolean watch)
310304
throws KeeperException, InterruptedException {
311-
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.getChildren")) {
312-
RetryCounter retryCounter = retryCounterFactory.create();
313-
while (true) {
314-
try {
315-
long startTime = EnvironmentEdgeManager.currentTime();
316-
List<String> children = checkZk().getChildren(path, watch);
317-
return children;
318-
} catch (KeeperException e) {
319-
switch (e.code()) {
320-
case CONNECTIONLOSS:
321-
retryOrThrow(retryCounter, e, "getChildren");
322-
break;
323-
case OPERATIONTIMEOUT:
324-
retryOrThrow(retryCounter, e, "getChildren");
325-
break;
326-
327-
default:
328-
throw e;
329-
}
330-
}
331-
retryCounter.sleepUntilNextRetry();
332-
}
333-
}
305+
return getChildren(path, null, watch);
334306
}
335307

336308
/**
@@ -339,12 +311,21 @@ public List<String> getChildren(String path, boolean watch)
339311
*/
340312
public byte[] getData(String path, Watcher watcher, Stat stat)
341313
throws KeeperException, InterruptedException {
314+
return getData(path, watcher, null, stat);
315+
}
316+
317+
private byte[] getData(String path, Watcher watcher, Boolean watch, Stat stat)
318+
throws InterruptedException, KeeperException {
342319
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.getData")) {
343320
RetryCounter retryCounter = retryCounterFactory.create();
344321
while (true) {
345322
try {
346-
long startTime = EnvironmentEdgeManager.currentTime();
347-
byte[] revData = checkZk().getData(path, watcher, stat);
323+
byte[] revData;
324+
if (watch == null) {
325+
revData = checkZk().getData(path, watcher, stat);
326+
} else {
327+
revData = checkZk().getData(path, watch, stat);
328+
}
348329
return ZKMetadata.removeMetaData(revData);
349330
} catch (KeeperException e) {
350331
switch (e.code()) {
@@ -370,29 +351,7 @@ public byte[] getData(String path, Watcher watcher, Stat stat)
370351
*/
371352
public byte[] getData(String path, boolean watch, Stat stat)
372353
throws KeeperException, InterruptedException {
373-
try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.getData")) {
374-
RetryCounter retryCounter = retryCounterFactory.create();
375-
while (true) {
376-
try {
377-
long startTime = EnvironmentEdgeManager.currentTime();
378-
byte[] revData = checkZk().getData(path, watch, stat);
379-
return ZKMetadata.removeMetaData(revData);
380-
} catch (KeeperException e) {
381-
switch (e.code()) {
382-
case CONNECTIONLOSS:
383-
retryOrThrow(retryCounter, e, "getData");
384-
break;
385-
case OPERATIONTIMEOUT:
386-
retryOrThrow(retryCounter, e, "getData");
387-
break;
388-
389-
default:
390-
throw e;
391-
}
392-
}
393-
retryCounter.sleepUntilNextRetry();
394-
}
395-
}
354+
return getData(path, null, watch, stat);
396355
}
397356

398357
/**
@@ -407,12 +366,9 @@ public Stat setData(String path, byte[] data, int version)
407366
RetryCounter retryCounter = retryCounterFactory.create();
408367
byte[] newData = ZKMetadata.appendMetaData(id, data);
409368
boolean isRetry = false;
410-
long startTime;
411369
while (true) {
412370
try {
413-
startTime = EnvironmentEdgeManager.currentTime();
414-
Stat nodeStat = checkZk().setData(path, newData, version);
415-
return nodeStat;
371+
return checkZk().setData(path, newData, version);
416372
} catch (KeeperException e) {
417373
switch (e.code()) {
418374
case CONNECTIONLOSS:
@@ -457,9 +413,7 @@ public List<ACL> getAcl(String path, Stat stat)
457413
RetryCounter retryCounter = retryCounterFactory.create();
458414
while (true) {
459415
try {
460-
long startTime = EnvironmentEdgeManager.currentTime();
461-
List<ACL> nodeACL = checkZk().getACL(path, stat);
462-
return nodeACL;
416+
return checkZk().getACL(path, stat);
463417
} catch (KeeperException e) {
464418
switch (e.code()) {
465419
case CONNECTIONLOSS:
@@ -488,9 +442,7 @@ public Stat setAcl(String path, List<ACL> acls, int version)
488442
RetryCounter retryCounter = retryCounterFactory.create();
489443
while (true) {
490444
try {
491-
long startTime = EnvironmentEdgeManager.currentTime();
492-
Stat nodeStat = checkZk().setACL(path, acls, version);
493-
return nodeStat;
445+
return checkZk().setACL(path, acls, version);
494446
} catch (KeeperException e) {
495447
switch (e.code()) {
496448
case CONNECTIONLOSS:
@@ -549,12 +501,9 @@ private String createNonSequential(String path, byte[] data, List<ACL> acl,
549501
CreateMode createMode) throws KeeperException, InterruptedException {
550502
RetryCounter retryCounter = retryCounterFactory.create();
551503
boolean isRetry = false; // False for first attempt, true for all retries.
552-
long startTime;
553504
while (true) {
554505
try {
555-
startTime = EnvironmentEdgeManager.currentTime();
556-
String nodePath = checkZk().create(path, data, acl, createMode);
557-
return nodePath;
506+
return checkZk().create(path, data, acl, createMode);
558507
} catch (KeeperException e) {
559508
switch (e.code()) {
560509
case NODEEXISTS:
@@ -608,9 +557,7 @@ private String createSequential(String path, byte[] data,
608557
}
609558
}
610559
first = false;
611-
long startTime = EnvironmentEdgeManager.currentTime();
612-
String nodePath = checkZk().create(newPath, data, acl, createMode);
613-
return nodePath;
560+
return checkZk().create(newPath, data, acl, createMode);
614561
} catch (KeeperException e) {
615562
switch (e.code()) {
616563
case CONNECTIONLOSS:
@@ -666,9 +613,7 @@ public List<OpResult> multi(Iterable<Op> ops)
666613
Iterable<Op> multiOps = prepareZKMulti(ops);
667614
while (true) {
668615
try {
669-
long startTime = EnvironmentEdgeManager.currentTime();
670-
List<OpResult> opResults = checkZk().multi(multiOps);
671-
return opResults;
616+
return checkZk().multi(multiOps);
672617
} catch (KeeperException e) {
673618
switch (e.code()) {
674619
case CONNECTIONLOSS:
@@ -693,12 +638,10 @@ private String findPreviousSequentialNode(String path)
693638
assert(lastSlashIdx != -1);
694639
String parent = path.substring(0, lastSlashIdx);
695640
String nodePrefix = path.substring(lastSlashIdx+1);
696-
long startTime = EnvironmentEdgeManager.currentTime();
697641
List<String> nodes = checkZk().getChildren(parent, false);
698642
List<String> matching = filterByPrefix(nodes, nodePrefix);
699643
for (String node : matching) {
700644
String nodePath = parent + "/" + node;
701-
startTime = EnvironmentEdgeManager.currentTime();
702645
Stat stat = checkZk().exists(nodePath, false);
703646
if (stat != null) {
704647
return nodePath;
@@ -725,10 +668,6 @@ public synchronized ZooKeeper getZooKeeper() {
725668
return zk;
726669
}
727670

728-
public synchronized byte[] getSessionPasswd() {
729-
return zk == null ? null : zk.getSessionPasswd();
730-
}
731-
732671
public void sync(String path, AsyncCallback.VoidCallback cb, Object ctx) throws KeeperException {
733672
checkZk().sync(path, cb, null);
734673
}

0 commit comments

Comments
 (0)