From a27be9fdbbf2d271c9c5780ba70fe15a24dbdb63 Mon Sep 17 00:00:00 2001 From: "Nelson,Thomas(tn1381)(arthurdent3)" Date: Tue, 29 Jan 2019 15:55:37 -0500 Subject: Push variuos changes - Spring Boot - Cadi - Cassandra Locking Change-Id: Ie9882f81f0ca141bdb7862cdabf978481fcd7c4a Issue-ID: MUSIC-296,MUSIC-272 Signed-off-by: Nelson,Thomas(tn1381)(arthurdent3) --- examples/VotingApp/README.md | 1 + examples/VotingApp/pom.xml | 97 ++- .../VotingApp/src/main/java/main/JsonDelete.java | 53 +- .../VotingApp/src/main/java/main/JsonInsert.java | 63 +- .../VotingApp/src/main/java/main/JsonKeySpace.java | 51 +- .../VotingApp/src/main/java/main/JsonTable.java | 37 +- .../src/main/java/main/MusicConnector.java | 89 +-- examples/VotingApp/src/main/java/main/Util.java | 2 +- .../VotingApp/src/main/java/main/VotingApp.java | 857 +++++++++++---------- 9 files changed, 628 insertions(+), 622 deletions(-) (limited to 'examples') diff --git a/examples/VotingApp/README.md b/examples/VotingApp/README.md index bcd37b9b..94dcc80a 100644 --- a/examples/VotingApp/README.md +++ b/examples/VotingApp/README.md @@ -1,3 +1,4 @@ +Compatible with MUSIC 2.5.3 The voting app for MUSIC illustrates the features of MUSIC as a multi-site state management diff --git a/examples/VotingApp/pom.xml b/examples/VotingApp/pom.xml index 0c85e60b..cfe085e1 100644 --- a/examples/VotingApp/pom.xml +++ b/examples/VotingApp/pom.xml @@ -28,23 +28,23 @@ stated inside of the file. 0.0.1-SNAPSHOT - - - maven2-repository.java.net - Java.net Repository for Maven - http://download.java.net/maven/2/ - default - - + + + maven2-repository.java.net + Java.net Repository for Maven + http://download.java.net/maven/2/ + default + + - + - - com.sun.jersey - jersey-server - 1.9 - - + + com.sun.jersey + jersey-server + 1.9 + + com.sun.jersey jersey-json 1.18.1 @@ -54,46 +54,46 @@ stated inside of the file. genson 0.99 - - com.sun.jersey - jersey-client - 1.9 - + + com.sun.jersey + jersey-client + 1.9 + - - com.sun.jersey - jersey-json - 1.9 - - - + --> - - com.googlecode.json-simple - json-simple - 1.1 + + com.googlecode.json-simple + json-simple + 1.1 - - com.datastax.cassandra - cassandra-driver-core - 3.0.0 - - - org.apache.zookeeper - zookeeper - 3.4.6 - + + com.datastax.cassandra + cassandra-driver-core + 3.0.0 + + + org.apache.zookeeper + zookeeper + 3.4.6 + - - org.apache.maven.plugins - maven-shade-plugin - 3.1.1 - - + + org.apache.maven.plugins + maven-shade-plugin + 3.1.1 + + @@ -119,4 +119,3 @@ stated inside of the file. - diff --git a/examples/VotingApp/src/main/java/main/JsonDelete.java b/examples/VotingApp/src/main/java/main/JsonDelete.java index 9b75f875..ef44e00a 100644 --- a/examples/VotingApp/src/main/java/main/JsonDelete.java +++ b/examples/VotingApp/src/main/java/main/JsonDelete.java @@ -20,42 +20,43 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.util.ArrayList; import java.util.Map; public class JsonDelete { - + private ArrayList columns = null; private Map consistencyInfo; - public Map getConsistencyInfo() { - return consistencyInfo; - } + public Map getConsistencyInfo() { + return consistencyInfo; + } - public void setConsistencyInfo(Map consistencyInfo) { - this.consistencyInfo = consistencyInfo; - } + public void setConsistencyInfo(Map consistencyInfo) { + this.consistencyInfo = consistencyInfo; + } public ArrayList getColumns() { - return columns; - } - public void setColumns(ArrayList columns) { - this.columns = columns; - } - String ttl, timestamp; - - public String getTtl() { - return ttl; - } - public void setTtl(String ttl) { - this.ttl = ttl; - } - public String getTimestamp() { - return timestamp; - } - public void setTimestamp(String timestamp) { - this.timestamp = timestamp; - } + return columns; + } + public void setColumns(ArrayList columns) { + this.columns = columns; + } + String ttl, timestamp; + + public String getTtl() { + return ttl; + } + public void setTtl(String ttl) { + this.ttl = ttl; + } + public String getTimestamp() { + return timestamp; + } + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } } diff --git a/examples/VotingApp/src/main/java/main/JsonInsert.java b/examples/VotingApp/src/main/java/main/JsonInsert.java index a2099c16..a19a564c 100644 --- a/examples/VotingApp/src/main/java/main/JsonInsert.java +++ b/examples/VotingApp/src/main/java/main/JsonInsert.java @@ -20,48 +20,49 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.util.Map; import org.codehaus.jackson.annotate.JsonIgnoreProperties; public class JsonInsert { - + private Map values; String ttl, timestamp; - private Map row_specification; + private Map row_specification; private Map consistencyInfo; - public Map getConsistencyInfo() { - return consistencyInfo; - } + public Map getConsistencyInfo() { + return consistencyInfo; + } - public void setConsistencyInfo(Map consistencyInfo) { - this.consistencyInfo = consistencyInfo; - } + public void setConsistencyInfo(Map consistencyInfo) { + this.consistencyInfo = consistencyInfo; + } - public String getTtl() { - return ttl; - } - public void setTtl(String ttl) { - this.ttl = ttl; - } - public String getTimestamp() { - return timestamp; - } - public void setTimestamp(String timestamp) { - this.timestamp = timestamp; - } - public Map getValues() { - return values; - } - public void setValues(Map values) { - this.values = values; - } + public String getTtl() { + return ttl; + } + public void setTtl(String ttl) { + this.ttl = ttl; + } + public String getTimestamp() { + return timestamp; + } + public void setTimestamp(String timestamp) { + this.timestamp = timestamp; + } + public Map getValues() { + return values; + } + public void setValues(Map values) { + this.values = values; + } public Map getRow_specification() { - return row_specification; - } - public void setRow_specification(Map row_specification) { - this.row_specification = row_specification; - } + return row_specification; + } + public void setRow_specification(Map row_specification) { + this.row_specification = row_specification; + } } diff --git a/examples/VotingApp/src/main/java/main/JsonKeySpace.java b/examples/VotingApp/src/main/java/main/JsonKeySpace.java index ff7b6c67..240d18a3 100644 --- a/examples/VotingApp/src/main/java/main/JsonKeySpace.java +++ b/examples/VotingApp/src/main/java/main/JsonKeySpace.java @@ -20,6 +20,7 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.util.Map; @@ -27,32 +28,32 @@ import java.util.Map; public class JsonKeySpace { private Map replicationInfo; - private String durabilityOfWrites; + private String durabilityOfWrites; private Map consistencyInfo; - public Map getConsistencyInfo() { - return consistencyInfo; - } - - public void setConsistencyInfo(Map consistencyInfo) { - this.consistencyInfo = consistencyInfo; - } - - public Map getReplicationInfo() { - return replicationInfo; - } - - public void setReplicationInfo(Map replicationInfo) { - this.replicationInfo = replicationInfo; - } - - public String getDurabilityOfWrites() { - return durabilityOfWrites; - } - public void setDurabilityOfWrites(String durabilityOfWrites) { - this.durabilityOfWrites = durabilityOfWrites; - } - - + public Map getConsistencyInfo() { + return consistencyInfo; + } + + public void setConsistencyInfo(Map consistencyInfo) { + this.consistencyInfo = consistencyInfo; + } + + public Map getReplicationInfo() { + return replicationInfo; + } + + public void setReplicationInfo(Map replicationInfo) { + this.replicationInfo = replicationInfo; + } + + public String getDurabilityOfWrites() { + return durabilityOfWrites; + } + public void setDurabilityOfWrites(String durabilityOfWrites) { + this.durabilityOfWrites = durabilityOfWrites; + } + + } diff --git a/examples/VotingApp/src/main/java/main/JsonTable.java b/examples/VotingApp/src/main/java/main/JsonTable.java index 66171af2..f1124b07 100644 --- a/examples/VotingApp/src/main/java/main/JsonTable.java +++ b/examples/VotingApp/src/main/java/main/JsonTable.java @@ -20,36 +20,37 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.util.Map; public class JsonTable { private Map fields; - private Map properties; + private Map properties; private Map consistencyInfo; - public Map getConsistencyInfo() { - return consistencyInfo; - } + public Map getConsistencyInfo() { + return consistencyInfo; + } - public void setConsistencyInfo(Map consistencyInfo) { - this.consistencyInfo = consistencyInfo; - } + public void setConsistencyInfo(Map consistencyInfo) { + this.consistencyInfo = consistencyInfo; + } public Map getProperties() { - return properties; - } + return properties; + } - public void setProperties(Map properties) { - this.properties = properties; - } + public void setProperties(Map properties) { + this.properties = properties; + } - public Map getFields() { - return fields; - } + public Map getFields() { + return fields; + } - public void setFields(Map fields) { - this.fields = fields; - } + public void setFields(Map fields) { + this.fields = fields; + } } diff --git a/examples/VotingApp/src/main/java/main/MusicConnector.java b/examples/VotingApp/src/main/java/main/MusicConnector.java index d6ed20ba..ab4d895d 100644 --- a/examples/VotingApp/src/main/java/main/MusicConnector.java +++ b/examples/VotingApp/src/main/java/main/MusicConnector.java @@ -20,6 +20,7 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.io.IOException; @@ -28,49 +29,49 @@ import java.net.UnknownHostException; import java.util.Random; public class MusicConnector { - - //change this to point to relevant cluster - public String[] musicNodes; + + //change this to point to relevant cluster + public String[] musicNodes; - public MusicConnector(String[] musicNodes){ - this.musicNodes = musicNodes; - } - - private String getMusicNodeIp(){ - Random r = new Random(); - int index = r.nextInt(musicNodes.length); - return musicNodes[index]; - } - - public String getMusicNodeURL(){ - String musicurl = "http://"+getMusicNodeIp()+":8080/MUSIC/rest/v2"; - return musicurl; - } - - public boolean isHostUp(String serverAddress) { - Boolean isUp = false; - try { - InetAddress inet = InetAddress.getByName(serverAddress); - isUp = inet.isReachable(1000); - } catch (UnknownHostException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return isUp; - } - - /* - private static String getMusicNodeIp(){ - - //return "54.224.168.13"; - return bigSiteMusicNode; - String serverAddress; - serverAddress = agaveMusicNode; - while(isHostUp(serverAddress) != true) - serverAddress = toggle(serverAddress); - return serverAddress; - }*/ + public MusicConnector(String[] musicNodes){ + this.musicNodes = musicNodes; + } + + private String getMusicNodeIp(){ + Random r = new Random(); + int index = r.nextInt(musicNodes.length); + return musicNodes[index]; + } + + public String getMusicNodeURL(){ + String musicurl = "http://"+getMusicNodeIp()+":8080/MUSIC/rest/v2"; + return musicurl; + } + + public boolean isHostUp(String serverAddress) { + Boolean isUp = false; + try { + InetAddress inet = InetAddress.getByName(serverAddress); + isUp = inet.isReachable(1000); + } catch (UnknownHostException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return isUp; + } + + /* + private static String getMusicNodeIp(){ + + //return "54.224.168.13"; + return bigSiteMusicNode; + String serverAddress; + serverAddress = agaveMusicNode; + while(isHostUp(serverAddress) != true) + serverAddress = toggle(serverAddress); + return serverAddress; + }*/ } diff --git a/examples/VotingApp/src/main/java/main/Util.java b/examples/VotingApp/src/main/java/main/Util.java index aefa9a36..0add7a70 100644 --- a/examples/VotingApp/src/main/java/main/Util.java +++ b/examples/VotingApp/src/main/java/main/Util.java @@ -1,6 +1,6 @@ package main; public class Util { - public static final String version="1.0.0"; + public static final String version="1.0.0"; } diff --git a/examples/VotingApp/src/main/java/main/VotingApp.java b/examples/VotingApp/src/main/java/main/VotingApp.java index e58c324d..8846ba22 100644 --- a/examples/VotingApp/src/main/java/main/VotingApp.java +++ b/examples/VotingApp/src/main/java/main/VotingApp.java @@ -20,6 +20,7 @@ stated inside of the file. --------------------------------------------------------------------------- */ + package main; import java.io.BufferedReader; import java.io.IOException; @@ -40,474 +41,474 @@ import com.sun.jersey.api.client.config.DefaultClientConfig; import com.sun.jersey.api.json.JSONConfiguration; public class VotingApp { - String keyspaceName; - ArrayList lockNames; - MusicConnector musicHandle; - private final String version="1.0.0"; - - //UPDATE your onboarding information here - String namespace = "votingapp"; - String userId = "abc123d"; - String password = "password"; - - public VotingApp(String[] musicIps){ - lockNames = new ArrayList(); - musicHandle = new MusicConnector(musicIps); - bootStrap(); - } - - /** - * Adds MUSIC's authentication headers into the webresource - * @param webResource - */ - private Builder addMusicHeaders(WebResource webResource) { - Builder builder = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON); - if (!namespace.equals("")) { - builder.header("ns", namespace); - } - if (!userId.equals("")) { - builder.header("userId", userId); - } - if (!password.equals("")) { - builder.header("password", password); - } - - return builder; - } - - public void createVotingKeyspace(){ - keyspaceName = "VotingAppForMusic"; - System.out.println("Voting app version "+ version+" ....."); - Map replicationInfo = new HashMap(); - replicationInfo.put("class", "SimpleStrategy"); - replicationInfo.put("replication_factor", 1); - String durabilityOfWrites="false"; - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "eventual"); - JsonKeySpace jsonKp = new JsonKeySpace(); - jsonKp.setConsistencyInfo(consistencyInfo); - jsonKp.setDurabilityOfWrites(durabilityOfWrites); - jsonKp.setReplicationInfo(replicationInfo); - - ClientConfig clientConfig = new DefaultClientConfig(); - - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - - WebResource webResource = client - .resource(musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName); - - ClientResponse response = addMusicHeaders(webResource).accept("application/json") - .type("application/json").post(ClientResponse.class, jsonKp); - if (response.getStatus() < 200 || (response.getStatus() > 299 && response.getStatus()!=400)) { //supress keyspace already exists - Map map = response.getEntity(Map.class); - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus() + "- " + map); - } - - } - - public void createVotingTable(){ - Map fields = new HashMap(); - fields.put("name", "text"); - fields.put("count", "varint"); - fields.put("PRIMARY KEY", "(name)"); - - - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "eventual"); - - JsonTable jtab = new JsonTable(); - jtab.setFields(fields); - jtab.setConsistencyInfo(consistencyInfo); - - ClientConfig clientConfig = new DefaultClientConfig(); - - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount"; - System.out.println("create url:"+url); - WebResource webResource = client - .resource(url); - - ClientResponse response = addMusicHeaders(webResource).accept("application/json") - .type("application/json").post(ClientResponse.class, jtab); - - System.out.println(response.getEntity(Map.class)); - if (response.getStatus() < 200 || (response.getStatus() > 299 && response.getStatus()!=400)) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); - - } - private void checkMusicVersion(){ - Client client = Client.create(); - System.out.println(musicHandle.getMusicNodeURL()+"/version"); - - -// System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); - WebResource webResource = client - .resource(musicHandle.getMusicNodeURL()+"/version"); - - - ClientResponse response = addMusicHeaders(webResource) - .accept(MediaType.APPLICATION_JSON).header("Connection", "close").get(ClientResponse.class); - - if (response.getStatus() != 200) { - throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); - } - - String output = response.getEntity(Map.class).toString(); - - System.out.println(output); - - } - - private void createEntryForCandidate(String candidateName){ - Map values = new HashMap(); - values.put("name",candidateName ); - values.put("count",0); - - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "eventual"); + String keyspaceName; + ArrayList lockNames; + MusicConnector musicHandle; + private final String version="1.0.0"; + + //UPDATE your onboarding information here + String namespace = "votingapp"; + String userId = "abc123d"; + String password = "password"; + + public VotingApp(String[] musicIps){ + lockNames = new ArrayList(); + musicHandle = new MusicConnector(musicIps); + bootStrap(); + } + + /** + * Adds MUSIC's authentication headers into the webresource + * @param webResource + */ + private Builder addMusicHeaders(WebResource webResource) { + Builder builder = webResource.accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON); + if (!namespace.equals("")) { + builder.header("ns", namespace); + } + if (!userId.equals("")) { + builder.header("userId", userId); + } + if (!password.equals("")) { + builder.header("password", password); + } + + return builder; + } + + public void createVotingKeyspace(){ + keyspaceName = "VotingAppForMusic"; + System.out.println("Voting app version "+ version+" ....."); + Map replicationInfo = new HashMap(); + replicationInfo.put("class", "SimpleStrategy"); + replicationInfo.put("replication_factor", 1); + String durabilityOfWrites="false"; + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "eventual"); + JsonKeySpace jsonKp = new JsonKeySpace(); + jsonKp.setConsistencyInfo(consistencyInfo); + jsonKp.setDurabilityOfWrites(durabilityOfWrites); + jsonKp.setReplicationInfo(replicationInfo); + + ClientConfig clientConfig = new DefaultClientConfig(); + + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + + WebResource webResource = client + .resource(musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName); + + ClientResponse response = addMusicHeaders(webResource).accept("application/json") + .type("application/json").post(ClientResponse.class, jsonKp); + if (response.getStatus() < 200 || (response.getStatus() > 299 && response.getStatus()!=400)) { //supress keyspace already exists + Map map = response.getEntity(Map.class); + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus() + "- " + map); + } + + } + + public void createVotingTable(){ + Map fields = new HashMap(); + fields.put("name", "text"); + fields.put("count", "varint"); + fields.put("PRIMARY KEY", "(name)"); + + + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "eventual"); + + JsonTable jtab = new JsonTable(); + jtab.setFields(fields); + jtab.setConsistencyInfo(consistencyInfo); + + ClientConfig clientConfig = new DefaultClientConfig(); + + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount"; + System.out.println("create url:"+url); + WebResource webResource = client + .resource(url); + + ClientResponse response = addMusicHeaders(webResource).accept("application/json") + .type("application/json").post(ClientResponse.class, jtab); + + System.out.println(response.getEntity(Map.class)); + if (response.getStatus() < 200 || (response.getStatus() > 299 && response.getStatus()!=400)) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); + + } + private void checkMusicVersion(){ + Client client = Client.create(); + System.out.println(musicHandle.getMusicNodeURL()+"/version"); + + +// System.setProperty("sun.net.http.allowRestrictedHeaders", "true"); + WebResource webResource = client + .resource(musicHandle.getMusicNodeURL()+"/version"); + + + ClientResponse response = addMusicHeaders(webResource) + .accept(MediaType.APPLICATION_JSON).header("Connection", "close").get(ClientResponse.class); + + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + String output = response.getEntity(Map.class).toString(); + + System.out.println(output); + + } + + private void createEntryForCandidate(String candidateName){ + Map values = new HashMap(); + values.put("name",candidateName ); + values.put("count",0); + + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "eventual"); - JsonInsert jIns = new JsonInsert(); - jIns.setValues(values); - jIns.setConsistencyInfo(consistencyInfo); - ClientConfig clientConfig = new DefaultClientConfig(); + JsonInsert jIns = new JsonInsert(); + jIns.setValues(values); + jIns.setConsistencyInfo(consistencyInfo); + ClientConfig clientConfig = new DefaultClientConfig(); - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - Client client = Client.create(clientConfig); + Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows"; - WebResource webResource = client - .resource(url); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows"; + WebResource webResource = client + .resource(url); - ClientResponse response = addMusicHeaders(webResource).accept("application/json") - .type("application/json").post(ClientResponse.class, jIns); + ClientResponse response = addMusicHeaders(webResource).accept("application/json") + .type("application/json").post(ClientResponse.class, jIns); - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+url+" candidate name:"+ candidateName); + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+url+" candidate name:"+ candidateName); - } + } - private String createLock(String lockName){ - Client client = Client.create(); - String msg = musicHandle.getMusicNodeURL()+"/locks/create/"+lockName; - WebResource webResource = client.resource(msg); - System.out.println(msg); - WebResource.Builder wb = addMusicHeaders(webResource).accept(MediaType.APPLICATION_JSON); + private String createLock(String lockName){ + Client client = Client.create(); + String msg = musicHandle.getMusicNodeURL()+"/locks/create/"+lockName; + WebResource webResource = client.resource(msg); + System.out.println(msg); + WebResource.Builder wb = addMusicHeaders(webResource).accept(MediaType.APPLICATION_JSON); - ClientResponse response = wb.post(ClientResponse.class); + ClientResponse response = wb.post(ClientResponse.class); - if (response.getStatus() != 200) { - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+msg); - } + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+msg); + } - Map responseMap = response.getEntity(Map.class); + Map responseMap = response.getEntity(Map.class); - String lockid = ((Map) responseMap.get("lock")).get("lock"); -// System.out.println("Server response .... \n"); -// System.out.println(output); - return lockid; - } + String lockid = ((Map) responseMap.get("lock")).get("lock"); +// System.out.println("Server response .... \n"); +// System.out.println(output); + return lockid; + } - private boolean acquireLock(String lockId){ - Client client = Client.create(); - String msg = musicHandle.getMusicNodeURL()+"/locks/acquire/"+lockId; - System.out.println(msg); - WebResource webResource = client.resource(msg); + private boolean acquireLock(String lockId){ + Client client = Client.create(); + String msg = musicHandle.getMusicNodeURL()+"/locks/acquire/"+lockId; + System.out.println(msg); + WebResource webResource = client.resource(msg); - WebResource.Builder wb = addMusicHeaders(webResource).accept(MediaType.APPLICATION_JSON); + WebResource.Builder wb = addMusicHeaders(webResource).accept(MediaType.APPLICATION_JSON); - ClientResponse response = wb.get(ClientResponse.class); + ClientResponse response = wb.get(ClientResponse.class); - Map responseMap = response.getEntity(Map.class); - - if (response.getStatus() != 200) { - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+ ":" + responseMap); - } + Map responseMap = response.getEntity(Map.class); + + if (response.getStatus() != 200) { + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+ ":" + responseMap); + } - System.out.println(responseMap); - Boolean status = responseMap.get("status").equals("SUCCESS"); - System.out.println("Server response .... " + status); - return status; - } + System.out.println(responseMap); + Boolean status = responseMap.get("status").equals("SUCCESS"); + System.out.println("Server response .... " + status); + return status; + } - private void unlock(String lockId){ - Client client = Client.create(); - WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/locks/release/"+lockId); + private void unlock(String lockId){ + Client client = Client.create(); + WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/locks/release/"+lockId); - ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); + ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); - if (response.getStatus() < 200 || response.getStatus()>299) { - throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); - } - } + if (response.getStatus() < 200 || response.getStatus()>299) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + } - private void updateVoteCountAtomically(String candidateName,int count){ - /*create lock for the candidate. The music API dictates that - * the lock name must be of the form keyspacename.tableName.primaryKeyName - * */ - System.out.println("trying to acquire lock!"); + private void updateVoteCountAtomically(String candidateName,int count){ + /*create lock for the candidate. The music API dictates that + * the lock name must be of the form keyspacename.tableName.primaryKeyName + * */ + System.out.println("trying to acquire lock!"); - String lockName = keyspaceName+".votecount."+candidateName; - lockNames.add(lockName); - String lockId = createLock(lockName); - while(acquireLock(lockId) != true); - - System.out.println("acquired lock!"); - //update candidate entry if you have the lock - Map values = new HashMap(); - values.put("count",count); + String lockName = keyspaceName+".votecount."+candidateName; + lockNames.add(lockName); + String lockId = createLock(lockName); + while(acquireLock(lockId) != true); + + System.out.println("acquired lock!"); + //update candidate entry if you have the lock + Map values = new HashMap(); + values.put("count",count); - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "critical"); - consistencyInfo.put("lockId", lockId); + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "critical"); + consistencyInfo.put("lockId", lockId); - JsonInsert jIns = new JsonInsert(); - jIns.setValues(values); - jIns.setConsistencyInfo(consistencyInfo); - ClientConfig clientConfig = new DefaultClientConfig(); + JsonInsert jIns = new JsonInsert(); + jIns.setValues(values); + jIns.setConsistencyInfo(consistencyInfo); + ClientConfig clientConfig = new DefaultClientConfig(); - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; - System.out.println(url); - WebResource webResource = client - .resource(url); + Client client = Client.create(clientConfig); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; + System.out.println(url); + WebResource webResource = client + .resource(url); - ClientResponse response = addMusicHeaders(webResource).accept("application/json") - .type("application/json").put(ClientResponse.class, jIns); + ClientResponse response = addMusicHeaders(webResource).accept("application/json") + .type("application/json").put(ClientResponse.class, jIns); - Map map = response.getEntity(Map.class); - - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+":"+map); - - //release lock now that the operation is done - unlock(lockId); - - } - - private void deleteCandidateEntryEventually(String candidateName){ - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "eventual"); + Map map = response.getEntity(Map.class); + + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+":"+map); + + //release lock now that the operation is done + unlock(lockId); + + } + + private void deleteCandidateEntryEventually(String candidateName){ + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "eventual"); - JsonDelete jDel = new JsonDelete(); - jDel.setConsistencyInfo(consistencyInfo); - ClientConfig clientConfig = new DefaultClientConfig(); + JsonDelete jDel = new JsonDelete(); + jDel.setConsistencyInfo(consistencyInfo); + ClientConfig clientConfig = new DefaultClientConfig(); - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; - System.out.println(url); - WebResource webResource = client - .resource(url); + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; + System.out.println(url); + WebResource webResource = client + .resource(url); - ClientResponse response = addMusicHeaders(webResource).accept("application/json") - .type("application/json").delete(ClientResponse.class, jDel); + ClientResponse response = addMusicHeaders(webResource).accept("application/json") + .type("application/json").delete(ClientResponse.class, jDel); - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+url); + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()+"url:"+url); - } - - public Map readVoteCountForCandidate(String candidateName){ - ClientConfig clientConfig = new DefaultClientConfig(); - - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; - WebResource webResource = client - .resource(url); - - ClientResponse response = addMusicHeaders(webResource).accept("application/json").get(ClientResponse.class); - - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); - - Map output = response.getEntity(Map.class); - return output; - } + } + + public Map readVoteCountForCandidate(String candidateName){ + ClientConfig clientConfig = new DefaultClientConfig(); + + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows?name="+candidateName; + WebResource webResource = client + .resource(url); + + ClientResponse response = addMusicHeaders(webResource).accept("application/json").get(ClientResponse.class); + + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); + + Map output = response.getEntity(Map.class); + return output; + } - public Map readAllVotes(){ - ClientConfig clientConfig = new DefaultClientConfig(); - - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows"; - WebResource webResource = client - .resource(url); - - ClientResponse response = addMusicHeaders(webResource).accept("application/json").get(ClientResponse.class); + public Map readAllVotes(){ + ClientConfig clientConfig = new DefaultClientConfig(); + + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + String url = musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName+"/tables/votecount/rows"; + WebResource webResource = client + .resource(url); + + ClientResponse response = addMusicHeaders(webResource).accept("application/json").get(ClientResponse.class); - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); - - Map output = response.getEntity(Map.class); - return output; - } - - - /* - * Unable to use this because of the error: - * Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: - * HTTP method DELETE doesn't support output. Seems to be a error in the rest java combination according to the interwebs - */ - private void dropKeySpace(){ - Map consistencyInfo= new HashMap(); - consistencyInfo.put("type", "eventual"); + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); + + Map output = response.getEntity(Map.class); + return output; + } + + + /* + * Unable to use this because of the error: + * Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: + * HTTP method DELETE doesn't support output. Seems to be a error in the rest java combination according to the interwebs + */ + private void dropKeySpace(){ + Map consistencyInfo= new HashMap(); + consistencyInfo.put("type", "eventual"); - JsonKeySpace jsonKp = new JsonKeySpace(); - jsonKp.setConsistencyInfo(consistencyInfo); + JsonKeySpace jsonKp = new JsonKeySpace(); + jsonKp.setConsistencyInfo(consistencyInfo); - ClientConfig clientConfig = new DefaultClientConfig(); + ClientConfig clientConfig = new DefaultClientConfig(); - clientConfig.getFeatures().put( - JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); - - Client client = Client.create(clientConfig); - - WebResource webResource = client - .resource(musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName); - - ClientResponse response = addMusicHeaders(webResource).type("application/json") - .delete(ClientResponse.class, jsonKp); - - if (response.getStatus() < 200 || response.getStatus() > 299) - throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); - } - - private void deleteLock(String lockName){ - Client client = Client.create(); - WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/locks/delete/"+lockName); - - ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); - - - if (response.getStatus() <200 || response.getStatus()>299) { - throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); - } - } - - private void resetMusic(){ - Client client = Client.create(); - WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/reset"); - - ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); - - - if (response.getStatus() != 204) { - throw new RuntimeException("Failed : HTTP error code : " - + response.getStatus()); - } - - } - public void deleteAllLocks(){ - for (String lockName : lockNames) { - deleteLock(lockName); - } - } - - - public void bootStrap(){ - checkMusicVersion(); - createVotingKeyspace(); - - - createVotingTable(); - - - //the next few lines just create an entry in the voting table for all these candidates with vote count as 0 - createEntryForCandidate("Popeye"); - - createEntryForCandidate("Judy"); - - createEntryForCandidate("Flash"); - - createEntryForCandidate("Mickey"); - - } - - public void overAllTests(){ - //update the count atomically - updateVoteCountAtomically("Popeye",5); - - updateVoteCountAtomically("Judy",7); - - updateVoteCountAtomically("Mickey",8); - - updateVoteCountAtomically("Flash",2); - - - //read votecount - System.out.println(readAllVotes()); - - System.out.println(readVoteCountForCandidate("Popeye")); - - System.out.println(readVoteCountForCandidate("Flash")); - - deleteCandidateEntryEventually("Mickey"); - - System.out.println(readAllVotes()); - -// dropKeySpace(); - - deleteAllLocks(); - } - - public void flipTest(){ - checkMusicVersion(); - } - - public static String executeBashScript(String pathToScript, String arg1, String arg2){ - try { - ProcessBuilder pb = new ProcessBuilder(pathToScript,arg1, arg2); - final Process process = pb.start(); - InputStream is = process.getInputStream(); - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); - return br.readLine(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } - - public static void main(String[] args) { - long start = System.currentTimeMillis(); - - if (args.length==0) { - args = new String[]{"localhost"}; - } - for(int i =0; i < 2;++i){ - VotingApp vHandle = new VotingApp(args); - vHandle.overAllTests(); - - System.out.println("====================================="); - System.out.println("Test no."+i+" completed:"); - } - long diff = System.currentTimeMillis() - start; - System.out.println(diff); - } - + clientConfig.getFeatures().put( + JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE); + + Client client = Client.create(clientConfig); + + WebResource webResource = client + .resource(musicHandle.getMusicNodeURL()+"/keyspaces/"+keyspaceName); + + ClientResponse response = addMusicHeaders(webResource).type("application/json") + .delete(ClientResponse.class, jsonKp); + + if (response.getStatus() < 200 || response.getStatus() > 299) + throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); + } + + private void deleteLock(String lockName){ + Client client = Client.create(); + WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/locks/delete/"+lockName); + + ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); + + + if (response.getStatus() <200 || response.getStatus()>299) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + } + + private void resetMusic(){ + Client client = Client.create(); + WebResource webResource = client.resource(musicHandle.getMusicNodeURL()+"/reset"); + + ClientResponse response = addMusicHeaders(webResource).delete(ClientResponse.class); + + + if (response.getStatus() != 204) { + throw new RuntimeException("Failed : HTTP error code : " + + response.getStatus()); + } + + } + public void deleteAllLocks(){ + for (String lockName : lockNames) { + deleteLock(lockName); + } + } + + + public void bootStrap(){ + checkMusicVersion(); + createVotingKeyspace(); + + + createVotingTable(); + + + //the next few lines just create an entry in the voting table for all these candidates with vote count as 0 + createEntryForCandidate("Popeye"); + + createEntryForCandidate("Judy"); + + createEntryForCandidate("Flash"); + + createEntryForCandidate("Mickey"); + + } + + public void overAllTests(){ + //update the count atomically + updateVoteCountAtomically("Popeye",5); + + updateVoteCountAtomically("Judy",7); + + updateVoteCountAtomically("Mickey",8); + + updateVoteCountAtomically("Flash",2); + + + //read votecount + System.out.println(readAllVotes()); + + System.out.println(readVoteCountForCandidate("Popeye")); + + System.out.println(readVoteCountForCandidate("Flash")); + + deleteCandidateEntryEventually("Mickey"); + + System.out.println(readAllVotes()); + +// dropKeySpace(); + + deleteAllLocks(); + } + + public void flipTest(){ + checkMusicVersion(); + } + + public static String executeBashScript(String pathToScript, String arg1, String arg2){ + try { + ProcessBuilder pb = new ProcessBuilder(pathToScript,arg1, arg2); + final Process process = pb.start(); + InputStream is = process.getInputStream(); + InputStreamReader isr = new InputStreamReader(is); + BufferedReader br = new BufferedReader(isr); + return br.readLine(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) { + long start = System.currentTimeMillis(); + + if (args.length==0) { + args = new String[]{"localhost"}; + } + for(int i =0; i < 2;++i){ + VotingApp vHandle = new VotingApp(args); + vHandle.overAllTests(); + + System.out.println("====================================="); + System.out.println("Test no."+i+" completed:"); + } + long diff = System.currentTimeMillis() - start; + System.out.println(diff); + } + } \ No newline at end of file -- cgit 1.2.3-korg