Database Reference
In-Depth Information
A reliable configuration service
Going back to the write() method in ActiveKeyValueStore , recall that it is com-
posed of an exists operation followed by either a create or a setData :
public void write ( String path , String value ) throws
InterruptedException ,
KeeperException {
Stat stat = zk . exists ( path , false );
if ( stat == null ) {
zk . create ( path , value . getBytes ( CHARSET ), Ids . OPEN_ACL_UNSAFE ,
CreateMode . PERSISTENT );
} else {
zk . setData ( path , value . getBytes ( CHARSET ), - 1 );
}
}
Taken as a whole, the write() method is idempotent, so we can afford to uncondition-
ally retry it. Here's a modified version of the write() method that retries in a loop. It is
set to try a maximum number of retries ( MAX_RETRIES ) and sleeps for
RETRY_PERIOD_SECONDS between each attempt:
public void write ( String path , String value ) throws
InterruptedException ,
KeeperException {
int retries = 0 ;
while ( true ) {
try {
Stat stat = zk . exists ( path , false );
if ( stat == null ) {
zk . create ( path , value . getBytes ( CHARSET ),
Ids . OPEN_ACL_UNSAFE ,
CreateMode . PERSISTENT );
} else {
zk . setData ( path , value . getBytes ( CHARSET ),
stat . getVersion ());
}
return ;
} catch ( KeeperException . SessionExpiredException e ) {
throw e ;
} catch ( KeeperException e ) {
if ( retries ++ == MAX_RETRIES ) {
throw e ;
}
// sleep then retry
TimeUnit . SECONDS . sleep ( RETRY_PERIOD_SECONDS );
Search WWH ::




Custom Search