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 cef615f..70f90ee 100644 (file)
@@ -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);
+
 }
index abd1cd5..bed66a2 100644 (file)
@@ -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);
+       }
 }
index 3e6caca..c87c2e9 100644 (file)
@@ -46,6 +46,10 @@ class LastMessage extends Entry {
        byte getType() {
                return Entry.TypeLastMessage;
        }
+
+       Entry getCopy(Slot s) {
+               return new LastMessage(s, machineid, seqnum);
+       }
 }
 
 
index 0e8ec4f..9c84f18 100644 (file)
@@ -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);
+       }
 }
index 6604894..4db9133 100644 (file)
@@ -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<numentries; i++) {
-                       slot.addEntry(Entry.decode(slot, bb));
+                       slot.addShallowEntry(Entry.decode(slot, bb));
                }
 
                return slot;
@@ -152,10 +159,11 @@ class Slot implements Liveness {
 
        Vector<Entry> getLiveEntries() {
                Vector<Entry> liveEntries=new Vector<Entry>();
-               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<Entry> e=getLiveEntries();
        }
 
        /**
index baf171d..26fc7a1 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);
@@ -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<Entry> 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<Entry> 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<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");
@@ -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);
-
                for(Entry entry : slot.getEntries()) {
                        switch(entry.getType()) {
                        case Entry.TypeKeyValue:
index d77e9f2..62f3a6d 100644 (file)
@@ -38,4 +38,8 @@ class TableStatus extends Entry {
        byte getType() {
                return Entry.TypeTableStatus;
        }
+
+       Entry getCopy(Slot s) {
+               return new TableStatus(s, maxslots);
+       }
 }
index c77702d..ad976ac 100644 (file)
@@ -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 (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;
-       }
-}