Skip to content

Commit

Permalink
Merge pull request #6 from musalbas/master
Browse files Browse the repository at this point in the history
upstream
  • Loading branch information
asonnino authored Jul 19, 2017
2 parents 9bb55c1 + de2fecd commit 7866633
Show file tree
Hide file tree
Showing 67 changed files with 1,842 additions and 234 deletions.
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
.idea
out
*.iml
venv
target
*.pyc
.cache
*.egg-info
.DS_Store
venv
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ public class ObjectStatus {
public static final String ACTIVE = "0";
public static final String LOCKED = "1";
public static final String INACTIVE = "2";

public static int mapObjectToShard(String object) {
int iObject = Integer.parseInt(object);
if(iObject%2 == 0)
return 0;
else
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ public class RequestType {
public static final int GET = 2;
public static final int REMOVE = 3;
public static final int SIZE = 4;
public static final int TRANSACTION = 5;
public static final String PREPARE_T = "prepare_t";
public static final String ACCEPT_T_COMMIT = "accept_t_commit";
public static final String ACCEPT_T_ABORT = "accept_t_abort";
public static final int TRANSACTION_VALIDITY = 5;
public static final int TRANSACTION_COMMIT = 6;
public static final int PREPARE_T = 7;
public static final int ACCEPT_T_ABORT = 8;
public static final int ACCEPT_T_COMMIT = 9;
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
import java.util.TreeMap;

public class Transaction implements Serializable {
private List<String> inputs;
private List<String> outputs;
public String id;
public List<String> inputs;
public List<String> outputs;

// Transaction states
public static final String VALID = "valid";
public static final String INVALID_NOOBJECT = "Invalid: Input object(s) doesn't exist.";
public static final String INVALID_NOMANAGEDOBJECT = "Invalid: None of the input object(s) is managed by this shard.";
public static final String REJECTED_LOCKEDOBJECT = "Rejected: Input object(s) is locked. ";
public static final String INVALID_INACTIVEOBJECT = "Invalid: Input object(s) is inactive.";
public static final String INVALID_BADTRANSACTION = "Invalid: Malformed transaction.";
Expand All @@ -30,6 +32,10 @@ public Transaction() {
outputs = new ArrayList<>();
}

public void addID(String id) {
this.id = id;
}

public void addInput(String in) {
inputs.add(in);
}
Expand Down Expand Up @@ -76,19 +82,31 @@ public static Transaction fromByteArray(byte[] data) {
}
}

public String getStatus(TreeMap<String, String> table) {
public String getStatus(TreeMap<String, String> table, int shard) {
// TODO: Check if the transaction is malformed, return INVALID_BADTRANSACTION

// Check if all input objects are active
for(String str: inputs) {
String readValue = table.get(str);
if(readValue == null)
// and at least one of the input objects is managed by this shard
int nManagedObj = 0;

for(String key: inputs) {
String readValue = table.get(key);
boolean managedObj = (ObjectStatus.mapObjectToShard(key)==shard);
if(managedObj)
nManagedObj++;
if(managedObj && readValue == null)
return INVALID_NOOBJECT;
else if(readValue.equals(ObjectStatus.LOCKED))
return REJECTED_LOCKEDOBJECT;
else if (readValue.equals(ObjectStatus.INACTIVE))
return INVALID_INACTIVEOBJECT;
else if(managedObj && readValue != null) {
if (readValue.equals(ObjectStatus.LOCKED))
return REJECTED_LOCKEDOBJECT;
else if (managedObj && readValue.equals(ObjectStatus.INACTIVE))
return INVALID_INACTIVEOBJECT;
}
}
// The case when this shard doesn't manage any of the input objects
if(nManagedObj == 0)
return INVALID_NOMANAGEDOBJECT;

return VALID;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foo.gettingstarted.client;

import foo.gettingstarted.RequestType;
import foo.gettingstarted.Transaction;

import java.util.Scanner;
Expand All @@ -14,7 +15,7 @@ public static void main(String[] args) {

MapClient[] clients = new MapClient[2];
clients[0] = new MapClient(0, "config0" );
clients[1] = new MapClient(1, "config1" );
//clients[1] = new MapClient(1, "config1" );

Scanner sc = new Scanner(System.in);
Scanner console = new Scanner(System.in);
Expand All @@ -30,13 +31,15 @@ public static void main(String[] args) {
System.out.println("2. READ A VALUE FROM THE MAP");
System.out.println("3. REMOVE AND ENTRY FROM THE MAP");
System.out.println("4. GET THE SIZE OF THE MAP");
System.out.println("5. SUBMIT A TRANSACTION");
System.out.println("5. VALIDATE A TRANSACTION");
System.out.println("6. COMMIT A TRANSACTION");
System.out.println("7. PREPARE_T");

cmd = sc.nextInt();
String key;
String key, input;

switch (cmd) {
case 1:
case RequestType.PUT:
System.out.println("Putting value in the map");
System.out.println("Enter the key:");
key = console.nextLine();
Expand All @@ -45,30 +48,35 @@ public static void main(String[] args) {
String result = client.put(key, value);
System.out.println("Previous value: " + result);
break;
case 2:
case RequestType.GET:
System.out.println("Reading value from the map");
System.out.println("Enter the key:");
key = console.nextLine();
result = client.get(key);
System.out.println("Value read: " + result);
break;
case 3:
case RequestType.REMOVE:
System.out.println("Removing value in the map");
System.out.println("Enter the key:");
key = console.nextLine();
result = client.remove(key);
System.out.println("Value removed: " + result);
break;
case 4:
case RequestType.SIZE:
System.out.println("Getting the map size");
int size = client.size();
System.out.println("Map size: " + size);
break;
case 5:
case RequestType.TRANSACTION_VALIDITY:
System.out.println("Checking if a transaction is valid");
Transaction tran = new Transaction();

System.out.println("Enter transaction ID:");
input = console.nextLine();
tran.addID(input);

System.out.println("Enter inputs, one per line (type 'q' to stop):");
String input = console.nextLine();
input = console.nextLine();

while (!(input.equalsIgnoreCase("q"))) {
tran.addInput(input);
Expand All @@ -84,9 +92,68 @@ public static void main(String[] args) {

System.out.println("Transaction to be added is:");
tran.print();
result = client.doTransaction(tran);
result = client.checkTransactionValidity(tran);
System.out.println("Transaction status: " + result);
break;

case RequestType.TRANSACTION_COMMIT:
System.out.println("Committing a transaction");
Transaction t = new Transaction();

System.out.println("Enter transaction ID:");
input = console.nextLine();
t.addID(input);

System.out.println("Enter inputs, one per line (type 'q' to stop):");
input = console.nextLine();

while (!(input.equalsIgnoreCase("q"))) {
t.addInput(input);
input=console.nextLine();
}

System.out.println("Enter outputs, one per line (type 'q' to stop):");
input=console.nextLine();
while (!(input.equalsIgnoreCase("q"))) {
t.addOutput(input);
input=console.nextLine();
}

System.out.println("Transaction to be added is:");
t.print();
result = client.commitTransaction(t);
System.out.println("Transaction status: " + result);
break;

case RequestType.PREPARE_T:
System.out.println("Doing PREPARE_T");
Transaction tt = new Transaction();

System.out.println("Enter transaction ID:");
input = console.nextLine();
tt.addID(input);

System.out.println("Enter inputs, one per line (type 'q' to stop):");
input = console.nextLine();

while (!(input.equalsIgnoreCase("q"))) {
tt.addInput(input);
input=console.nextLine();
}

System.out.println("Enter outputs, one per line (type 'q' to stop):");
input=console.nextLine();
while (!(input.equalsIgnoreCase("q"))) {
tt.addOutput(input);
input=console.nextLine();
}

System.out.println("Transaction to be added is:");
tt.print();
result = client.prepare_t(tt);
System.out.println("Transaction status: " + result);
break;

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class MapClient implements Map<String, String> {

ServiceProxy clientProxy = null;


public MapClient(int clientId, String pathConfig) {

//clientProxy = new ServiceProxy(clientId);
Expand Down Expand Up @@ -141,12 +142,12 @@ public int size() {
}
}

public String doTransaction(Transaction t) {
public String checkTransactionValidity(Transaction t) {

try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bs);
oos.writeInt(RequestType.TRANSACTION);
oos.writeInt(RequestType.TRANSACTION_VALIDITY);
oos.writeObject(t);
oos.close();
byte[] reply = clientProxy.invokeUnordered(bs.toByteArray());
Expand All @@ -159,6 +160,43 @@ public String doTransaction(Transaction t) {
return null;
}
}
}

public String commitTransaction(Transaction t) {

try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bs);
oos.writeInt(RequestType.TRANSACTION_COMMIT);
oos.writeObject(t);
oos.close();
byte[] reply = clientProxy.invokeUnordered(bs.toByteArray());
if (reply != null) {
return new String(reply, Charset.forName("UTF-8"));
}
return null;
} catch (IOException ioe) {
System.out.println("Exception: " + ioe.getMessage());
return null;
}
}

public String prepare_t(Transaction t) {

try {
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bs);
oos.writeInt(RequestType.PREPARE_T);
oos.writeObject(t);
oos.close();
byte[] reply = clientProxy.invokeOrdered(bs.toByteArray());
if (reply != null) {
return new String(reply, Charset.forName("UTF-8"));
}
return null;
} catch (IOException ioe) {
System.out.println("Exception: " + ioe.getMessage());
return null;
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@ public class ObjectStatus {
public static final String ACTIVE = "0";
public static final String LOCKED = "1";
public static final String INACTIVE = "2";

public static int mapObjectToShard(String object) {
int iObject = Integer.parseInt(object);
if(iObject%2 == 0)
return 0;
else
return 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ public class RequestType {
public static final int GET = 2;
public static final int REMOVE = 3;
public static final int SIZE = 4;
public static final int TRANSACTION = 5;
public static final String PREPARE_T = "prepare_t";
public static final String ACCEPT_T_COMMIT = "accept_t_commit";
public static final String ACCEPT_T_ABORT = "accept_t_abort";
public static final int TRANSACTION_VALIDITY = 5;
public static final int TRANSACTION_COMMIT = 6;
public static final int PREPARE_T = 7;
public static final int ACCEPT_T_ABORT = 8;
public static final int ACCEPT_T_COMMIT = 9;
}
Loading

0 comments on commit 7866633

Please sign in to comment.