Madan Jampani

More checkstyle fixes.

......@@ -228,76 +228,89 @@ public class DatabaseManager implements DatabaseService, DatabaseAdminService {
@Override
public boolean putIfAbsent(String tableName, String key, byte[] value) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().putIfAbsent(tableName, key, value).build();
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder()
.putIfAbsent(tableName, key, value).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return true;
} else if (writeResult.status().equals(WriteStatus.PRECONDITION_VIOLATION)) {
return false;
}
throw new DatabaseException("putIfAbsent failed due to status: " + writeResult.status());
throw new DatabaseException("putIfAbsent failed due to status: "
+ writeResult.status());
}
@Override
public boolean putIfVersionMatches(String tableName, String key,
byte[] value, long version) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().putIfVersionMatches(tableName, key, value, version).build();
BatchWriteRequest batchRequest =
new BatchWriteRequest.Builder()
.putIfVersionMatches(tableName, key, value, version).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return true;
} else if (writeResult.status().equals(WriteStatus.PRECONDITION_VIOLATION)) {
return false;
}
throw new DatabaseException("putIfVersionMatches failed due to status: " + writeResult.status());
throw new DatabaseException("putIfVersionMatches failed due to status: "
+ writeResult.status());
}
@Override
public boolean putIfValueMatches(String tableName, String key,
byte[] oldValue, byte[] newValue) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().putIfValueMatches(tableName, key, oldValue, newValue).build();
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder()
.putIfValueMatches(tableName, key, oldValue, newValue).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return true;
} else if (writeResult.status().equals(WriteStatus.PRECONDITION_VIOLATION)) {
return false;
}
throw new DatabaseException("putIfValueMatches failed due to status: " + writeResult.status());
throw new DatabaseException("putIfValueMatches failed due to status: "
+ writeResult.status());
}
@Override
public VersionedValue remove(String tableName, String key) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().remove(tableName, key).build();
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder()
.remove(tableName, key).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return writeResult.previousValue();
}
throw new DatabaseException("remove failed due to status: " + writeResult.status());
throw new DatabaseException("remove failed due to status: "
+ writeResult.status());
}
@Override
public boolean removeIfVersionMatches(String tableName, String key,
long version) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().removeIfVersionMatches(tableName, key, version).build();
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder()
.removeIfVersionMatches(tableName, key, version).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return true;
} else if (writeResult.status().equals(WriteStatus.PRECONDITION_VIOLATION)) {
return false;
}
throw new DatabaseException("removeIfVersionMatches failed due to status: " + writeResult.status());
throw new DatabaseException("removeIfVersionMatches failed due to status: "
+ writeResult.status());
}
@Override
public boolean removeIfValueMatches(String tableName, String key,
byte[] value) {
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder().removeIfValueMatches(tableName, key, value).build();
BatchWriteRequest batchRequest = new BatchWriteRequest.Builder()
.removeIfValueMatches(tableName, key, value).build();
WriteResult writeResult = batchWrite(batchRequest).getAsList().get(0);
if (writeResult.status().equals(WriteStatus.OK)) {
return true;
} else if (writeResult.status().equals(WriteStatus.PRECONDITION_VIOLATION)) {
return false;
}
throw new DatabaseException("removeIfValueMatches failed due to status: " + writeResult.status());
throw new DatabaseException("removeIfValueMatches failed due to status: "
+ writeResult.status());
}
@Override
......
......@@ -47,7 +47,7 @@ public class DatabaseStateMachine implements StateMachine {
private final Logger log = getLogger(getClass());
// message subject for database update notifications.
public static MessageSubject DATABASE_UPDATE_EVENTS =
public static final MessageSubject DATABASE_UPDATE_EVENTS =
new MessageSubject("database-update-events");
// serializer used for snapshot
......
......@@ -38,11 +38,16 @@ import org.onlab.onos.store.service.DatabaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DatabaseUpdateEventHandler implements DatabaseUpdateEventListener, EventHandler<LeaderElectEvent> {
/**
* Database update event handler.
*/
public class DatabaseUpdateEventHandler implements
DatabaseUpdateEventListener, EventHandler<LeaderElectEvent> {
private final Logger log = LoggerFactory.getLogger(getClass());
public final static MessageSubject DATABASE_UPDATES = new MessageSubject("database-update-event");
public static final MessageSubject DATABASE_UPDATES =
new MessageSubject("database-update-event");
private DatabaseService databaseService;
private ClusterService cluster;
......
......@@ -16,23 +16,26 @@
package org.onlab.onos.store.service.impl;
/**
* Interface of database update event listeners.
*/
public interface DatabaseUpdateEventListener {
/**
*
* @param event
* Notifies listeners of a table modified event.
* @param event table modification event.
*/
public void tableModified(TableModificationEvent event);
/**
*
* Notifies listeners of a table created event.
* @param tableName
* @param expirationTimeMillis
*/
public void tableCreated(String tableName, int expirationTimeMillis);
/**
*
* Notifies listeners of a table deleted event.
* @param tableName
*/
public void tableDeleted(String tableName);
......
......@@ -69,7 +69,7 @@ public class DistributedLock implements Lock {
public boolean tryLock(
long waitTimeMillis,
int leaseDurationMillis) {
if (tryLock(leaseDurationMillis) == false) {
if (!tryLock(leaseDurationMillis)) {
CompletableFuture<Void> future =
lockManager.lockIfAvailable(this, waitTimeMillis, leaseDurationMillis);
try {
......
......@@ -80,7 +80,10 @@ public class DistributedLockManager implements LockService {
throw new UnsupportedOperationException();
}
protected CompletableFuture<Void> lockIfAvailable(Lock lock, long waitTimeMillis, int leaseDurationMillis) {
protected CompletableFuture<Void> lockIfAvailable(
Lock lock,
long waitTimeMillis,
int leaseDurationMillis) {
CompletableFuture<Void> future = new CompletableFuture<>();
locksToAcquire.put(
lock.path(),
......@@ -103,7 +106,9 @@ public class DistributedLockManager implements LockService {
if (event.type() == TableModificationEvent.Type.ROW_DELETED) {
List<LockRequest> existingRequests = locksToAcquire.get(path);
if (existingRequests == null) return;
if (existingRequests == null) {
return;
}
Iterator<LockRequest> existingRequestIterator = existingRequests.iterator();
while (existingRequestIterator.hasNext()) {
......@@ -111,7 +116,7 @@ public class DistributedLockManager implements LockService {
if (request.expirationTime().isAfter(DateTime.now())) {
existingRequestIterator.remove();
} else {
if (request.lock().tryLock(request.leaseDurationMillis()) == true) {
if (request.lock().tryLock(request.leaseDurationMillis())) {
request.future().complete(null);
existingRequests.remove(0);
}
......
package org.onlab.onos.store.service.impl;
public class TableModificationEvent {
/**
* A table modification event.
*/
public final class TableModificationEvent {
/**
* Type of table modification event.
*
*/
public enum Type {
ROW_ADDED,
ROW_DELETED,
......@@ -12,14 +19,32 @@ public class TableModificationEvent {
private final String key;
private final Type type;
/**
* Creates a new row deleted table modification event.
* @param tableName table name.
* @param key row key
* @return table modification event.
*/
public static TableModificationEvent rowDeleted(String tableName, String key) {
return new TableModificationEvent(tableName, key, Type.ROW_DELETED);
}
/**
* Creates a new row added table modification event.
* @param tableName table name.
* @param key row key
* @return table modification event.
*/
public static TableModificationEvent rowAdded(String tableName, String key) {
return new TableModificationEvent(tableName, key, Type.ROW_ADDED);
}
/**
* Creates a new row updated table modification event.
* @param tableName table name.
* @param key row key
* @return table modification event.
*/
public static TableModificationEvent rowUpdated(String tableName, String key) {
return new TableModificationEvent(tableName, key, Type.ROW_UPDATED);
}
......@@ -30,14 +55,26 @@ public class TableModificationEvent {
this.type = type;
}
/**
* Returns name of table this event is for.
* @return table name
*/
public String tableName() {
return tableName;
}
/**
* Returns the row key this event is for.
* @return row key
*/
public String key() {
return key;
}
/**
* Returns the type of table modification event.
* @return event type.
*/
public Type type() {
return type;
}
......