bug fixes
authorBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Mon, 25 Jul 2016 06:12:16 +0000 (23:12 -0700)
committerBrian Demsky <bdemsky@plrg.eecs.uci.edu>
Mon, 25 Jul 2016 06:12:16 +0000 (23:12 -0700)
src/java/iotcloud/Entry.java
src/java/iotcloud/KeyValue.java
src/java/iotcloud/LastMessage.java
src/java/iotcloud/RejectedMessage.java
src/java/iotcloud/Slot.java
src/java/iotcloud/Table.java
src/java/iotcloud/TableStatus.java
src/java/iotcloud/Test.java
src/java/iotcloud/TestCloudComm.java [deleted file]

index cef615f7d8c83df68f69c02a84f9980795ff3a5a..70f90ee60e7f57d072a76f15e569466df9d34cf1 100644 (file)
@@ -85,4 +85,10 @@ abstract class Entry implements Liveness {
         */
 
        abstract byte getType();
         */
 
        abstract byte getType();
+
+       /**
+        * Returns a copy of the Entry that can be added to a different slot.
+        */
+       abstract Entry getCopy(Slot s);
+
 }
 }
index abd1cd5f2243e25138c0ed9adc8201eb45147798..bed66a2cf436bcc56effb8f7c5f3f7eb726110eb 100644 (file)
@@ -50,4 +50,12 @@ class KeyValue extends Entry {
        byte getType() {
                return Entry.TypeKeyValue;
        }
        byte getType() {
                return Entry.TypeKeyValue;
        }
+
+       public String toString() {
+               return value.toString();
+       }
+
+       Entry getCopy(Slot s) {
+               return new KeyValue(s, key, value);
+       }
 }
 }
index 3e6caca04f5705833ff97c5bc52c1e67c170757e..c87c2e97494f7e4bbbea8681ce741fda35526f85 100644 (file)
@@ -46,6 +46,10 @@ class LastMessage extends Entry {
        byte getType() {
                return Entry.TypeLastMessage;
        }
        byte getType() {
                return Entry.TypeLastMessage;
        }
+
+       Entry getCopy(Slot s) {
+               return new LastMessage(s, machineid, seqnum);
+       }
 }
 
 
 }
 
 
index 0e8ec4f4bc4a55bc24452dc22a3970499646958c..9c84f18e053720b43de5e617684ba512d828b2fd 100644 (file)
@@ -81,4 +81,8 @@ class RejectedMessage extends Entry {
        byte getType() {
                return Entry.TypeRejectedMessage;
        }
        byte getType() {
                return Entry.TypeRejectedMessage;
        }
+       
+       Entry getCopy(Slot s) {
+               return new RejectedMessage(s, machineid, oldseqnum, newseqnum, equalto);
+       }
 }
 }
index 6604894e0e364e3cf556f254ac828ba4a74394d6..4db9133de5a958ec5a9a6ba523fd41297ddb5600 100644 (file)
@@ -64,6 +64,13 @@ class Slot implements Liveness {
        }
 
        void addEntry(Entry e) {
        }
 
        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();
                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<numentries; i++) {
                Slot slot=new Slot(seqnum, machineid, prevhmac, hmac);
 
                for(int i=0; i<numentries; i++) {
-                       slot.addEntry(Entry.decode(slot, bb));
+                       slot.addShallowEntry(Entry.decode(slot, bb));
                }
 
                return slot;
                }
 
                return slot;
@@ -152,10 +159,11 @@ class Slot implements Liveness {
 
        Vector<Entry> getLiveEntries() {
                Vector<Entry> liveEntries=new Vector<Entry>();
 
        Vector<Entry> getLiveEntries() {
                Vector<Entry> liveEntries=new Vector<Entry>();
-               for(Entry entry: entries)
+               for(Entry entry: entries) {
                        if (entry.isLive())
                                liveEntries.add(entry);
                        if (entry.isLive())
                                liveEntries.add(entry);
-
+               }
+                       
                if (seqnumlive)
                        liveEntries.add(new LastMessage(this, machineid, seqnum));
 
                if (seqnumlive)
                        liveEntries.add(new LastMessage(this, machineid, seqnum));
 
@@ -184,8 +192,8 @@ class Slot implements Liveness {
         */
 
        void setDead() {
         */
 
        void setDead() {
-               decrementLiveCount();
                seqnumlive=false;
                seqnumlive=false;
+               decrementLiveCount();
        }
 
        /**
        }
 
        /**
@@ -194,6 +202,7 @@ class Slot implements Liveness {
 
        void decrementLiveCount() {
                livecount--;
 
        void decrementLiveCount() {
                livecount--;
+               Vector<Entry> e=getLiveEntries();
        }
 
        /**
        }
 
        /**
index baf171d7725c8c28a2879feee44ffc7816c09ed1..26fc7a1802c6db014f63979fd24c9746f0e45a24 100644 (file)
@@ -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);
        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) {
                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 */
                        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);
                        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;
                                if (!prevslot.isLive())
                                        continue;
-                               System.out.println("islive");
                                Vector<Entry> liveentries = prevslot.getLiveEntries();
                                for(Entry liveentry:liveentries) {
                                Vector<Entry> liveentries = prevslot.getLiveEntries();
                                for(Entry liveentry:liveentries) {
-                                       if (redundant(liveentry))
-                                               continue;
                                        if (s.hasSpace(liveentry))
                                                s.addEntry(liveentry);
                                        else if (i==0) {
                                        if (s.hasSpace(liveentry))
                                                s.addEntry(liveentry);
                                        else if (i==0) {
@@ -139,8 +137,6 @@ search:
                                continue;
                        Vector<Entry> liveentries = prevslot.getLiveEntries();
                        for(Entry liveentry:liveentries) {
                                continue;
                        Vector<Entry> liveentries = prevslot.getLiveEntries();
                        for(Entry liveentry:liveentries) {
-                               if (redundant(liveentry))
-                                       continue;
                                if (s.hasSpace(liveentry))
                                        s.addEntry(liveentry);
                                else
                                if (s.hasSpace(liveentry))
                                        s.addEntry(liveentry);
                                else
@@ -163,15 +159,6 @@ search:
                return insertedkv;
        }
 
                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 */
        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<Long, Liveness> lastmsgentry = lastmessagetable.put(machineid, new Pair<Long, Liveness>(seqnum, liveness));
                if (lastmsgentry == null)
                        return;
 
                long lastmsgseqnum = lastmsgentry.getFirst();
                Liveness lastentry = lastmsgentry.getSecond();
                Pair<Long, Liveness> lastmsgentry = lastmessagetable.put(machineid, new Pair<Long, Liveness>(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");
                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<Long> machineSet) {
                updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet);
 
        private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptupdatestolocal, HashSet<Long> machineSet) {
                updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptupdatestolocal, machineSet);
-
                for(Entry entry : slot.getEntries()) {
                        switch(entry.getType()) {
                        case Entry.TypeKeyValue:
                for(Entry entry : slot.getEntries()) {
                        switch(entry.getType()) {
                        case Entry.TypeKeyValue:
index d77e9f25135063aa8deab9535e4dc435caa385ce..62f3a6dac96ef51439c15222fbe694d6ad816524 100644 (file)
@@ -38,4 +38,8 @@ class TableStatus extends Entry {
        byte getType() {
                return Entry.TypeTableStatus;
        }
        byte getType() {
                return Entry.TypeTableStatus;
        }
+
+       Entry getCopy(Slot s) {
+               return new TableStatus(s, maxslots);
+       }
 }
 }
index c77702ddb78e025ae56438c45efb4e413df4fbdf..ad976ac1111d423b99fa7f03689ec9923885aed1 100644 (file)
@@ -8,30 +8,38 @@ package iotcloud;
 
 public class Test {
        public static void main(String[] args) {
 
 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();
                        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() {
        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);
        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));
                }
        }
                        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 (file)
index 9c752a8..0000000
+++ /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 <bdemsky@uci.edu>
- * @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<numslots; i++,sequencenumber++)
-                       slots[i]=buffer.getSlot(sequencenumber);
-               return slots;
-       }
-}