From fd550d0d3aee01650e0121c6e849befbc7e58f1d Mon Sep 17 00:00:00 2001 From: Brian Demsky Date: Sun, 24 Jul 2016 23:12:16 -0700 Subject: [PATCH 1/1] bug fixes --- src/java/iotcloud/Entry.java | 6 +++ src/java/iotcloud/KeyValue.java | 8 ++++ src/java/iotcloud/LastMessage.java | 4 ++ src/java/iotcloud/RejectedMessage.java | 4 ++ src/java/iotcloud/Slot.java | 17 +++++-- src/java/iotcloud/Table.java | 52 ++++++++++----------- src/java/iotcloud/TableStatus.java | 4 ++ src/java/iotcloud/Test.java | 62 +++++++++----------------- src/java/iotcloud/TestCloudComm.java | 41 ----------------- 9 files changed, 85 insertions(+), 113 deletions(-) delete mode 100644 src/java/iotcloud/TestCloudComm.java diff --git a/src/java/iotcloud/Entry.java b/src/java/iotcloud/Entry.java index cef615f..70f90ee 100644 --- a/src/java/iotcloud/Entry.java +++ b/src/java/iotcloud/Entry.java @@ -85,4 +85,10 @@ abstract class Entry implements Liveness { */ abstract byte getType(); + + /** + * Returns a copy of the Entry that can be added to a different slot. + */ + abstract Entry getCopy(Slot s); + } diff --git a/src/java/iotcloud/KeyValue.java b/src/java/iotcloud/KeyValue.java index abd1cd5..bed66a2 100644 --- a/src/java/iotcloud/KeyValue.java +++ b/src/java/iotcloud/KeyValue.java @@ -50,4 +50,12 @@ class KeyValue extends Entry { byte getType() { return Entry.TypeKeyValue; } + + public String toString() { + return value.toString(); + } + + Entry getCopy(Slot s) { + return new KeyValue(s, key, value); + } } diff --git a/src/java/iotcloud/LastMessage.java b/src/java/iotcloud/LastMessage.java index 3e6caca..c87c2e9 100644 --- a/src/java/iotcloud/LastMessage.java +++ b/src/java/iotcloud/LastMessage.java @@ -46,6 +46,10 @@ class LastMessage extends Entry { byte getType() { return Entry.TypeLastMessage; } + + Entry getCopy(Slot s) { + return new LastMessage(s, machineid, seqnum); + } } diff --git a/src/java/iotcloud/RejectedMessage.java b/src/java/iotcloud/RejectedMessage.java index 0e8ec4f..9c84f18 100644 --- a/src/java/iotcloud/RejectedMessage.java +++ b/src/java/iotcloud/RejectedMessage.java @@ -81,4 +81,8 @@ class RejectedMessage extends Entry { byte getType() { return Entry.TypeRejectedMessage; } + + Entry getCopy(Slot s) { + return new RejectedMessage(s, machineid, oldseqnum, newseqnum, equalto); + } } diff --git a/src/java/iotcloud/Slot.java b/src/java/iotcloud/Slot.java index 6604894..4db9133 100644 --- a/src/java/iotcloud/Slot.java +++ b/src/java/iotcloud/Slot.java @@ -64,6 +64,13 @@ class Slot implements Liveness { } void addEntry(Entry e) { + e=e.getCopy(this); + entries.add(e); + livecount++; + freespace -= e.getSize(); + } + + private void addShallowEntry(Entry e) { entries.add(e); livecount++; freespace -= e.getSize(); @@ -109,7 +116,7 @@ class Slot implements Liveness { Slot slot=new Slot(seqnum, machineid, prevhmac, hmac); for(int i=0; i getLiveEntries() { Vector liveEntries=new Vector(); - for(Entry entry: entries) + for(Entry entry: entries) { if (entry.isLive()) liveEntries.add(entry); - + } + if (seqnumlive) liveEntries.add(new LastMessage(this, machineid, seqnum)); @@ -184,8 +192,8 @@ class Slot implements Liveness { */ void setDead() { - decrementLiveCount(); seqnumlive=false; + decrementLiveCount(); } /** @@ -194,6 +202,7 @@ class Slot implements Liveness { void decrementLiveCount() { livecount--; + Vector e=getLiveEntries(); } /** diff --git a/src/java/iotcloud/Table.java b/src/java/iotcloud/Table.java index baf171d..26fc7a1 100644 --- a/src/java/iotcloud/Table.java +++ b/src/java/iotcloud/Table.java @@ -75,6 +75,10 @@ final public class Table { } } + public String toString() { + return table.toString(); + } + public IoTString put(IoTString key, IoTString value) { while(true) { KeyValue oldvalue=table.get(key); @@ -92,26 +96,20 @@ final public class Table { long seqn = buffer.getOldestSeqNum(); if (forcedresize) { - System.out.println("A"); TableStatus status=new TableStatus(s, FORCED_RESIZE_INCREMENT + numslots); s.addEntry(status); } if ((numslots - buffer.size()) < FREE_SLOTS) { /* have to check whether we have enough free slots */ - System.out.println("B"); long fullfirstseqn = buffer.getNewestSeqNum() + 1 - numslots; seqn = fullfirstseqn < 1?1:fullfirstseqn; for(int i=0; i < FREE_SLOTS; i++, seqn++) { Slot prevslot=buffer.getSlot(seqn); - System.out.println(i); if (!prevslot.isLive()) continue; - System.out.println("islive"); Vector liveentries = prevslot.getLiveEntries(); for(Entry liveentry:liveentries) { - if (redundant(liveentry)) - continue; if (s.hasSpace(liveentry)) s.addEntry(liveentry); else if (i==0) { @@ -139,8 +137,6 @@ search: continue; Vector liveentries = prevslot.getLiveEntries(); for(Entry liveentry:liveentries) { - if (redundant(liveentry)) - continue; if (s.hasSpace(liveentry)) s.addEntry(liveentry); else @@ -163,15 +159,6 @@ search: return insertedkv; } - boolean redundant(Entry liveentry) { - if (liveentry.getType()==Entry.TypeLastMessage) { - LastMessage lastmsg=(LastMessage) liveentry; - return lastmsg.getMachineID() == localmachineid; - } - return false; - } - - private void validateandupdate(Slot[] newslots, boolean acceptupdatestolocal) { /* The cloud communication layer has checked slot HMACs already before decoding */ @@ -317,21 +304,35 @@ search: } } } - + + if (machineid == localmachineid) { + /* Our own messages are immediately dead. */ + if (liveness instanceof LastMessage) { + ((LastMessage)liveness).setDead(); + } else if (liveness instanceof Slot) { + ((Slot)liveness).setDead(); + } else { + throw new Error("Unrecognized type"); + } + } + + Pair lastmsgentry = lastmessagetable.put(machineid, new Pair(seqnum, liveness)); if (lastmsgentry == null) return; long lastmsgseqnum = lastmsgentry.getFirst(); Liveness lastentry = lastmsgentry.getSecond(); - if (lastentry instanceof LastMessage) { - ((LastMessage)lastentry).setDead(); - } else if (lastentry instanceof Slot) { - ((Slot)lastentry).setDead(); - } else { - throw new Error("Unrecognized type"); + if (machineid != localmachineid) { + if (lastentry instanceof LastMessage) { + ((LastMessage)lastentry).setDead(); + } else if (lastentry instanceof Slot) { + ((Slot)lastentry).setDead(); + } else { + throw new Error("Unrecognized type"); + } } - + if (machineid == localmachineid) { if (lastmsgseqnum != seqnum && !acceptupdatestolocal) throw new Error("Server Error: Mismatch on local machine sequence number"); @@ -343,7 +344,6 @@ search: private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptupdatestolocal, HashSet machineSet) { updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet); - for(Entry entry : slot.getEntries()) { switch(entry.getType()) { case Entry.TypeKeyValue: diff --git a/src/java/iotcloud/TableStatus.java b/src/java/iotcloud/TableStatus.java index d77e9f2..62f3a6d 100644 --- a/src/java/iotcloud/TableStatus.java +++ b/src/java/iotcloud/TableStatus.java @@ -38,4 +38,8 @@ class TableStatus extends Entry { byte getType() { return Entry.TypeTableStatus; } + + Entry getCopy(Slot s) { + return new TableStatus(s, maxslots); + } } diff --git a/src/java/iotcloud/Test.java b/src/java/iotcloud/Test.java index c77702d..ad976ac 100644 --- a/src/java/iotcloud/Test.java +++ b/src/java/iotcloud/Test.java @@ -8,30 +8,38 @@ package iotcloud; public class Test { public static void main(String[] args) { - if (args[0].equals("1")) - test1(); - else if(args[0].equals("2")) + if(args[0].equals("2")) test2(); else if(args[0].equals("3")) test3(); else if(args[0].equals("4")) test4(); + else if(args[0].equals("5")) + test5(); } + + static Thread buildThread(String prefix, Table t) { return new Thread() { - public void run() { - for(int i=0; i<600; i++) { - String a=prefix+i; - IoTString ia=new IoTString(a); - t.put(ia, ia); - System.out.println(ia+"->"+t.get(ia)); - } - } + public void run() { + for(int i=0; i<10000; i++) { + String a=prefix+i; + IoTString ia=new IoTString(a); + t.put(ia, ia); + System.out.println(ia+"->"+t.get(ia)); + } + } }; } - + + static void test5() { + Table t1=new Table("http://127.0.0.1/test.iotcloud/", "reallysecret", 321); + t1.rebuild(); + System.out.println(t1); + } + static void test4() { Table t1=new Table("http://127.0.0.1/test.iotcloud/", "reallysecret", 321); Table t2=new Table("http://127.0.0.1/test.iotcloud/", "reallysecret", 351); @@ -84,34 +92,4 @@ public class Test { System.out.println(ia+"->"+t2.get(ia)); } } - - static void test1() { - TestCloudComm cc=new TestCloudComm(); - Table t1=new Table(cc, 6513); - t1.initTable(); - Table t2=new Table(cc, 6512); - t2.update(); - for(int i=0; i<600; i++) { - String a="STR"+i; - String b="ABR"+i; - IoTString ia=new IoTString(a); - IoTString ib=new IoTString(b); - t1.put(ia, ia); - t2.put(ib, ib); - t1.update(); - System.out.println(ib+"->"+t1.get(ib)); - System.out.println(ia+"->"+t2.get(ia)); - } - for(int i=0; i<600; i++) { - String a="STR"+i; - String b="ABR"+i; - IoTString ia=new IoTString(a); - IoTString ib=new IoTString(b); - System.out.println(ib+"->"+t1.get(ib)); - System.out.println(ia+"->"+t2.get(ia)); - System.out.println(ib+"->"+t2.get(ib)); - System.out.println(ia+"->"+t1.get(ia)); - } - - } } diff --git a/src/java/iotcloud/TestCloudComm.java b/src/java/iotcloud/TestCloudComm.java deleted file mode 100644 index 9c752a8..0000000 --- a/src/java/iotcloud/TestCloudComm.java +++ /dev/null @@ -1,41 +0,0 @@ -package iotcloud; -import java.io.*; -import java.net.*; - -/** - * This class is a test driver to test the code w/o going through an - * actual web server. - * @author Brian Demsky - * @version 1.0 - */ - -class TestCloudComm extends CloudComm { - SlotBuffer buffer; - - TestCloudComm() { - buffer = new SlotBuffer(); - } - - public synchronized Slot[] putSlot(Slot slot, int max) { - if ((buffer.size()==0 && 1 == slot.getSequenceNumber()) || - buffer.getNewestSeqNum()+1 == slot.getSequenceNumber()) { - if (max!=0) - buffer.resize(max); - buffer.putSlot(slot); - return null; - } else - return getSlots(slot.getSequenceNumber()); - } - - public synchronized Slot[] getSlots(long sequencenumber) { - long newestseqnum=buffer.getNewestSeqNum(); - long oldestseqnum=buffer.getOldestSeqNum(); - if (sequencenumber < oldestseqnum) - sequencenumber=oldestseqnum; - int numslots=(int)((newestseqnum - sequencenumber)+1); - Slot[] slots=new Slot[numslots]; - for(int i=0; i