forked from apache/curator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into CURATOR-426
- Loading branch information
Showing
55 changed files
with
1,391 additions
and
357 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
curator-client/src/main/java/org/apache/curator/utils/Compatibility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* 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.curator.utils; | ||
|
||
import org.apache.zookeeper.ZooKeeper; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* Utils to help with ZK 3.4.x compatibility | ||
*/ | ||
public class Compatibility | ||
{ | ||
private static final boolean hasZooKeeperAdmin; | ||
static | ||
{ | ||
boolean hasIt; | ||
try | ||
{ | ||
Class.forName("org.apache.zookeeper.admin.ZooKeeperAdmin"); | ||
hasIt = true; | ||
} | ||
catch ( ClassNotFoundException e ) | ||
{ | ||
hasIt = false; | ||
LoggerFactory.getLogger(Compatibility.class).info("Running in ZooKeeper 3.4.x compatibility mode"); | ||
} | ||
hasZooKeeperAdmin = hasIt; | ||
} | ||
|
||
/** | ||
* Return true if the classpath ZooKeeper library is 3.4.x | ||
* | ||
* @return true/false | ||
*/ | ||
public static boolean isZK34() | ||
{ | ||
return !hasZooKeeperAdmin; | ||
} | ||
|
||
/** | ||
* For ZooKeeper 3.5.x, use the supported <code>zooKeeper.getTestable().injectSessionExpiration()</code>. | ||
* For ZooKeeper 3.4.x do the equivalent via reflection | ||
* | ||
* @param zooKeeper client | ||
*/ | ||
public static void injectSessionExpiration(ZooKeeper zooKeeper) | ||
{ | ||
if ( isZK34() ) | ||
{ | ||
InjectSessionExpiration.injectSessionExpiration(zooKeeper); | ||
} | ||
else | ||
{ | ||
// LOL - this method was proposed by me (JZ) in 2013 for totally unrelated reasons | ||
// it got added to ZK 3.5 and now does exactly what we need | ||
// https://issues.apache.org/jira/browse/ZOOKEEPER-1730 | ||
zooKeeper.getTestable().injectSessionExpiration(); | ||
} | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
curator-client/src/main/java/org/apache/curator/utils/InjectSessionExpiration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* 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.curator.utils; | ||
|
||
import org.apache.zookeeper.ClientCnxn; | ||
import org.apache.zookeeper.WatchedEvent; | ||
import org.apache.zookeeper.Watcher; | ||
import org.apache.zookeeper.ZooKeeper; | ||
import java.lang.reflect.Field; | ||
import java.lang.reflect.Method; | ||
|
||
// reflective version of zooKeeper.getTestable().injectSessionExpiration(); | ||
@SuppressWarnings("JavaReflectionMemberAccess") | ||
public class InjectSessionExpiration | ||
{ | ||
private static final Field cnxnField; | ||
private static final Field stateField; | ||
private static final Field eventThreadField; | ||
private static final Field sendThreadField; | ||
private static final Method queueEventMethod; | ||
private static final Method queueEventOfDeathMethod; | ||
private static final Method getClientCnxnSocketMethod; | ||
private static final Method wakeupCnxnMethod; | ||
static | ||
{ | ||
Field localCnxnField; | ||
Field localStateField; | ||
Field localEventThreadField; | ||
Field localSendThreadField; | ||
Method localQueueEventMethod; | ||
Method localEventOfDeathMethod; | ||
Method localGetClientCnxnSocketMethod; | ||
Method localWakeupCnxnMethod; | ||
try | ||
{ | ||
Class<?> eventThreadClass = Class.forName("org.apache.zookeeper.ClientCnxn$EventThread"); | ||
Class<?> sendThreadClass = Class.forName("org.apache.zookeeper.ClientCnxn$SendThread"); | ||
Class<?> clientCnxnSocketClass = Class.forName("org.apache.zookeeper.ClientCnxnSocket"); | ||
|
||
localCnxnField = ZooKeeper.class.getDeclaredField("cnxn"); | ||
localCnxnField.setAccessible(true); | ||
localStateField = ClientCnxn.class.getDeclaredField("state"); | ||
localStateField.setAccessible(true); | ||
localEventThreadField = ClientCnxn.class.getDeclaredField("eventThread"); | ||
localEventThreadField.setAccessible(true); | ||
localSendThreadField = ClientCnxn.class.getDeclaredField("sendThread"); | ||
localSendThreadField.setAccessible(true); | ||
localQueueEventMethod = eventThreadClass.getDeclaredMethod("queueEvent", WatchedEvent.class); | ||
localQueueEventMethod.setAccessible(true); | ||
localEventOfDeathMethod = eventThreadClass.getDeclaredMethod("queueEventOfDeath"); | ||
localEventOfDeathMethod.setAccessible(true); | ||
localGetClientCnxnSocketMethod = sendThreadClass.getDeclaredMethod("getClientCnxnSocket"); | ||
localGetClientCnxnSocketMethod.setAccessible(true); | ||
localWakeupCnxnMethod = clientCnxnSocketClass.getDeclaredMethod("wakeupCnxn"); | ||
localWakeupCnxnMethod.setAccessible(true); | ||
} | ||
catch ( ReflectiveOperationException e ) | ||
{ | ||
throw new RuntimeException("Could not access internal ZooKeeper fields", e); | ||
} | ||
cnxnField = localCnxnField; | ||
stateField = localStateField; | ||
eventThreadField = localEventThreadField; | ||
sendThreadField = localSendThreadField; | ||
queueEventMethod = localQueueEventMethod; | ||
queueEventOfDeathMethod = localEventOfDeathMethod; | ||
getClientCnxnSocketMethod = localGetClientCnxnSocketMethod; | ||
wakeupCnxnMethod = localWakeupCnxnMethod; | ||
} | ||
|
||
public static void injectSessionExpiration(ZooKeeper zooKeeper) | ||
{ | ||
try | ||
{ | ||
WatchedEvent event = new WatchedEvent(Watcher.Event.EventType.None, Watcher.Event.KeeperState.Expired, null); | ||
|
||
ClientCnxn clientCnxn = (ClientCnxn)cnxnField.get(zooKeeper); | ||
Object eventThread = eventThreadField.get(clientCnxn); | ||
queueEventMethod.invoke(eventThread, event); | ||
queueEventOfDeathMethod.invoke(eventThread); | ||
stateField.set(clientCnxn, ZooKeeper.States.CLOSED); | ||
Object sendThread = sendThreadField.get(clientCnxn); | ||
Object clientCnxnSocket = getClientCnxnSocketMethod.invoke(sendThread); | ||
wakeupCnxnMethod.invoke(clientCnxnSocket); | ||
} | ||
catch ( ReflectiveOperationException e ) | ||
{ | ||
throw new RuntimeException("Could not inject session expiration using reflection", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
curator-framework/src/main/java/org/apache/curator/framework/SafeIsTtlMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* 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.curator.framework; | ||
|
||
import org.apache.curator.utils.Compatibility; | ||
import org.apache.zookeeper.CreateMode; | ||
|
||
public class SafeIsTtlMode | ||
{ | ||
private static class Internal | ||
{ | ||
private static final Internal instance = new Internal(); | ||
|
||
public boolean isTtl(CreateMode mode) | ||
{ | ||
return mode.isTTL(); | ||
} | ||
} | ||
|
||
public static boolean isTtl(CreateMode mode) | ||
{ | ||
return !Compatibility.isZK34() && Internal.instance.isTtl(mode); | ||
} | ||
|
||
private SafeIsTtlMode() | ||
{ | ||
} | ||
} |
Oops, something went wrong.