Skip to content
Snippets Groups Projects
Commit e995c1e3 authored by allanbank's avatar allanbank
Browse files

Merge pull request #464 from allanbank/accumulo-cleanup

[accumulo] Checkstyle cleanup and enforcement for the Accumulo binding.
parents 19e3d270 f3160f6b
No related branches found
No related tags found
No related merge requests found
...@@ -64,4 +64,28 @@ LICENSE file. ...@@ -64,4 +64,28 @@ LICENSE file.
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>2.15</version>
<configuration>
<consoleOutput>true</consoleOutput>
<configLocation>../checkstyle.xml</configLocation>
<failOnViolation>true</failOnViolation>
<failsOnError>true</failsOnError>
</configuration>
<executions>
<execution>
<id>validate</id>
<phase>validate</phase>
<goals>
<goal>checkstyle</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>
...@@ -21,7 +21,6 @@ package com.yahoo.ycsb.db; ...@@ -21,7 +21,6 @@ package com.yahoo.ycsb.db;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.WatchedEvent;
...@@ -30,111 +29,165 @@ import org.apache.zookeeper.ZooKeeper; ...@@ -30,111 +29,165 @@ import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
// Implementing the PC Queue in ZooKeeper /**
// * Implementing the PC (Producer/Consumer) Queue in ZooKeeper.
*/
public class ZKProducerConsumer implements Watcher { public class ZKProducerConsumer implements Watcher {
static ZooKeeper zk = null; private static ZooKeeper zk = null;
static Integer mutex; private static Integer mutex;
String root; private String root;
// Constructor that takes tha address of the ZK server /**
// * Constructor that takes the address of the ZK server.
ZKProducerConsumer(String address) { *
if(zk == null){ * @param address
try { * The address of the ZK server.
System.out.println("Starting ZK:"); */
zk = new ZooKeeper(address, 3000, this); ZKProducerConsumer(String address) {
mutex = new Integer(-1); if (zk == null) {
System.out.println("Finished starting ZK: " + zk); try {
} catch (IOException e) { System.out.println("Starting ZK:");
System.out.println(e.toString()); zk = new ZooKeeper(address, 3000, this);
zk = null; mutex = new Integer(-1);
} System.out.println("Finished starting ZK: " + zk);
} } catch (IOException e) {
//else mutex = new Integer(-1); System.out.println(e.toString());
zk = null;
}
} }
// else mutex = new Integer(-1);
}
synchronized public void process(WatchedEvent event) { public synchronized void process(WatchedEvent event) {
synchronized (mutex) { synchronized (mutex) {
//System.out.println("Process: " + event.getType()); // System.out.println("Process: " + event.getType());
mutex.notify(); mutex.notify();
}
} }
}
static public class QueueElement { /**
public String key; * Returns the root.
public long writeTime; *
* @return The root.
QueueElement(String key, long writeTime) { */
this.key = key; protected String getRoot() {
this.writeTime = writeTime; return root;
} }
/**
* Sets the root.
*
* @param r
* The root value.
*/
protected void setRoot(String r) {
this.root = r;
}
/**
* QueueElement a single queue element. No longer used.
* @deprecated No longer used.
*/
@Deprecated
public static class QueueElement {
private String key;
private long writeTime;
QueueElement(String key, long writeTime) {
this.key = key;
this.writeTime = writeTime;
} }
}
// Producer-Consumer queue
static public class Queue extends ZKProducerConsumer { /**
* Producer-Consumer queue.
// Constructor of producer-consumer queue */
Queue(String address, String name) { public static class Queue extends ZKProducerConsumer {
super(address);
this.root = name; /**
// Create ZK node name * Constructor of producer-consumer queue.
if (zk != null) { *
try { * @param address
Stat s = zk.exists(root, false); * The Zookeeper server address.
if (s == null) { * @param name
zk.create(root, new byte[0], Ids.OPEN_ACL_UNSAFE, * The name of the root element for the queue.
CreateMode.PERSISTENT); */
} Queue(String address, String name) {
} catch (KeeperException e) { super(address);
System.out this.setRoot(name);
.println("Keeper exception when instantiating queue: " // Create ZK node name
+ e.toString()); if (zk != null) {
} catch (InterruptedException e) { try {
System.out.println("Interrupted exception"); Stat s = zk.exists(getRoot(), false);
} if (s == null) {
} zk.create(getRoot(), new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
} catch (KeeperException e) {
System.out.println(
"Keeper exception when instantiating queue: " + e.toString());
} catch (InterruptedException e) {
System.out.println("Interrupted exception");
} }
}
}
// Producer calls this method to insert the key in the queue /**
// * Producer calls this method to insert the key in the queue.
boolean produce(String key) throws KeeperException, InterruptedException{ *
byte[] value; * @param key
value = key.getBytes(); * The key to produce (add to the queue).
zk.create(root + "/key", value, * @return True if the key was added.
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL); * @throws KeeperException
* On a failure talking to zookeeper.
* @throws InterruptedException
* If the current thread is interrupted waiting for the zookeeper
* acknowledgement.
*/
//
boolean produce(String key) throws KeeperException, InterruptedException {
byte[] value;
value = key.getBytes();
zk.create(getRoot() + "/key", value, Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
return true; return true;
} }
// Consumer calls this method to "wait" for the key to the available /**
// * Consumer calls this method to "wait" for the key to the available.
String consume() throws KeeperException, InterruptedException { *
String retvalue = null; * @return The key to consumed (remove from the queue).
Stat stat = null; * @throws KeeperException
* On a failure talking to zookeeper.
// Get the first element available * @throws InterruptedException
while (true) { * If the current thread is interrupted waiting for the zookeeper
synchronized (mutex) { * acknowledgement.
List<String> list = zk.getChildren(root, true); */
if (list.size() == 0) { String consume() throws KeeperException, InterruptedException {
System.out.println("Going to wait"); String retvalue = null;
mutex.wait(); Stat stat = null;
} else {
String path = root+"/"+list.get(0); // Get the first element available
byte[] b = zk.getData(path, false, stat); while (true) {
retvalue = new String(b); synchronized (mutex) {
zk.delete(path, -1); List<String> list = zk.getChildren(getRoot(), true);
if (list.size() == 0) {
return retvalue; System.out.println("Going to wait");
mutex.wait();
} } else {
} String path = getRoot() + "/" + list.get(0);
} byte[] b = zk.getData(path, false, stat);
retvalue = new String(b);
zk.delete(path, -1);
return retvalue;
}
} }
}
} }
}
} }
/**
* Copyright (c) 2015 YCSB contributors. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
/**
* YCSB binding for <a href="https://accumulo.apache.org/">Accumulo</a>.
*/
package com.yahoo.ycsb.db;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment