From: navid Date: Fri, 21 Nov 2008 03:01:40 +0000 (+0000) Subject: *** empty log message *** X-Git-Tag: buildscript^7~17 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=3eb313ef3cf3c7a0c2634ee1d3b1361046aaaaef;p=IRC.git *** empty log message *** --- diff --git a/Robust/Transactions/TransactionalIOSrc/Utilities/Range.java b/Robust/Transactions/TransactionalIOSrc/Utilities/Range.java new file mode 100644 index 00000000..0de34e3d --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/Utilities/Range.java @@ -0,0 +1,108 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.Utilities; + + +/** + * + * @author navid + */ +public class Range implements Comparable { + + private long start; + private long end; + + public Range() { + } + + public Range(long start, long end) { + this.start = start; + this.end = end; + } + + public long getEnd() { + return end; + } + + public void setEnd(long end) { + this.end = end; + } + + public long getStart() { + return start; + } + + public void setStart(long start) { + this.start = start; + } + + public Range intersection(Range secondrange) { + if ((secondrange.start <= this.start) && (this.start <= secondrange.end)) { + return new Range(this.start, Math.min(this.end, secondrange.end)); + } else if ((secondrange.start <= this.end) && (this.end <= secondrange.end)) { + return new Range(Math.max(this.start, secondrange.start), this.end); + } else if ((this.start <= secondrange.start) && (secondrange.end <= this.end)) { + return new Range(secondrange.start, secondrange.end); + } else { + return null; + } + } + + public boolean hasIntersection(Range secondrange) { + if ((secondrange.start <= this.start) && (this.start <= secondrange.end)) { + return true; + } else if ((secondrange.start <= this.end) && (this.end <= secondrange.end)) { + return true; + } else if ((this.start <= secondrange.start) && (secondrange.end <= this.end)) { + return true; + } else { + return false; + } + } + + + public boolean includes(Range secondrange) { + if (this.start <= secondrange.start && secondrange.end <= this.end) { + return true; + } else { + return false; + } + } + + public Range[] minus(Range[] intersectedranges, int size) { + Range[] tmp = new Range[size + 1]; + + int counter = 0; + if (this.start < intersectedranges[0].start) { + tmp[counter] = new Range(this.start, intersectedranges[0].start); + counter++; + } + for (int i = 1; i < size; i++) { + tmp[counter] = new Range(intersectedranges[i - 1].end, intersectedranges[i].start); + counter++; + } + if (this.end > intersectedranges[size - 1].end) { + tmp[counter] = new Range(intersectedranges[size - 1].end, this.end); + counter++; + } + Range[] result = new Range[counter]; + for (int i = 0; i < counter; i++) { + result[i] = tmp[i]; + } + return result; + } + + public int compareTo(Object arg0) { + + Range tmp = (Range) arg0; + if (this.start < tmp.start) { + return -1; + } else if (this.start == tmp.start) { + return 0; + } else { + return 1; + } + } +} diff --git a/Robust/Transactions/TransactionalIOSrc/benchmarks/Main.java b/Robust/Transactions/TransactionalIOSrc/benchmarks/Main.java new file mode 100644 index 00000000..7797e908 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/benchmarks/Main.java @@ -0,0 +1,152 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.benchmarks; + +import TransactionalIO.core.CustomThread; +import TransactionalIO.core.ExtendedTransaction; +import TransactionalIO.core.TransactionalFile; +import java.io.File; +import java.util.Iterator; +import java.util.TreeMap; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author navid + */ +public class Main { + + public static void main(String args[]){ + try { + // benchmark.init(); + + + // for (int i=0; i<100; i++){ + benchmark.init(); + //System.out.println( + long starttime = System.nanoTime(); + /* thread1 tr1 = new thread1(); + thread2 tr2 = new thread2(); + thread3 tr3 = new thread3(); + thread4 tr4 = new thread4();*/ + /*TransactionalIO tr2 = new TransactionalIO(); + TransactionalIO tr3 = new TransactionalIO(); + CustomThread ct1 = new CustomThread(tr1); + CustomThread ct2 = new CustomThread(tr2);*/ + + /* CustomThread ct1 = new CustomThread(new thread1('a', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct2 = new CustomThread(new thread1('b', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct3 = new CustomThread(new thread1('c', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct4= new CustomThread(new thread1('d', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct5 = new CustomThread(new thread1('e', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct6 = new CustomThread(new thread1('f', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct7 = new CustomThread(new thread1('g', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct8 = new CustomThread(new thread1('h', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct9 = new CustomThread(new thread1('i',new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct10 = new CustomThread(new thread1('j', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct11 = new CustomThread(new thread1('k', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct12 = new CustomThread(new thread1('l', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct13 = new CustomThread(new thread1('m', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct14 = new CustomThread(new thread1('n', new TransactionalFile("/home/navid/output.text", "rw"))); + + CustomThread ct15 = new CustomThread(new thread1('o', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct16 = new CustomThread(new thread1('p', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct17 = new CustomThread(new thread1('q', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct18 = new CustomThread(new thread1('r', new TransactionalFile("/home/navid/output.text", "rw"))); + + // CustomThread ct15 = new CustomThread(tr2); + CustomThread ct19 = new CustomThread(new thread1('s', new TransactionalFile("/home/navid/output.text", "rw"))); + CustomThread ct20 = new CustomThread(new thread1('t', new TransactionalFile("/home/navid/output.text", "rw")));*/ + + + CustomThread ct1 = new CustomThread(new thread1('a')); + CustomThread ct2 = new CustomThread(new thread1('b')); + CustomThread ct3 = new CustomThread(new thread1('c')); + CustomThread ct4= new CustomThread(new thread1('d')); + CustomThread ct5 = new CustomThread(new thread1('e')); + CustomThread ct6 = new CustomThread(new thread1('f')); + CustomThread ct7 = new CustomThread(new thread1('g')); + CustomThread ct8 = new CustomThread(new thread1('h')); + CustomThread ct9 = new CustomThread(new thread1('i')); + CustomThread ct10 = new CustomThread(new thread1('j')); + CustomThread ct11 = new CustomThread(new thread1('k')); + CustomThread ct12 = new CustomThread(new thread1('l')); + CustomThread ct13 = new CustomThread(new thread1('m')); + CustomThread ct14 = new CustomThread(new thread1('n')); + + CustomThread ct15 = new CustomThread(new thread1('o')); + CustomThread ct16 = new CustomThread(new thread1('p')); + CustomThread ct17 = new CustomThread(new thread1('q')); + CustomThread ct18 = new CustomThread(new thread1('r')); + + // CustomThread ct15 = new CustomThread(tr2); + CustomThread ct19 = new CustomThread(new thread1('s')); + CustomThread ct20 = new CustomThread(new thread1('t')); + + + //CustomThread ct4 = new CustomThread(tr3); + + // CustomThread ct5 = new CustomThread(tr4); + // CustomThread ct6 = new CustomThread(tr2); + + // CustomThread ct4 = new CustomThread(tr2); + ct1.runner.join(); + ct2.runner.join(); + ct3.runner.join(); + ct4.runner.join(); + ct5.runner.join(); + ct6.runner.join(); + ct7.runner.join(); + ct8.runner.join(); + ct9.runner.join(); + ct10.runner.join(); + ct11.runner.join(); + ct12.runner.join(); + ct13.runner.join(); + ct14.runner.join(); + + + + ct15.runner.join(); + ct16.runner.join(); + ct17.runner.join(); + ct18.runner.join(); + ct19.runner.join(); + + ct20.runner.join(); + + long endttime = System.nanoTime(); + // System.out.println(endttime - starttime); + System.out.println((endttime - starttime)/1000000); + //} + /* TreeMap msgs = new TreeMap(); + Iterator it = benchmark.transacctions.iterator(); + while(it.hasNext()){ + ExtendedTransaction tr = (ExtendedTransaction) it.next(); + msgs.putAll(tr.msg); + } + + Iterator it2 = msgs.keySet().iterator(); + while(it2.hasNext()){ + Long time = (Long) it2.next(); + System.out.print(time +" " + msgs.get(time)); + }*/ + int index =97; + for (int j = 0; j < 26; j++) { + ((TransactionalFile)(benchmark.m.get(String.valueOf((char) (index+j))))).close(); + } + // } + + //System.out.println(Thread.currentThread().getName()); + } catch (InterruptedException ex) { + Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); + } + //System.out.println(Thread.currentThread().getName()); + + + } +} diff --git a/Robust/Transactions/TransactionalIOSrc/benchmarks/MainforLocks.java b/Robust/Transactions/TransactionalIOSrc/benchmarks/MainforLocks.java new file mode 100644 index 00000000..e74f4978 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/benchmarks/MainforLocks.java @@ -0,0 +1,142 @@ + +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.benchmarks; + +import TransactionalIO.core.TransactionalFile; +import java.io.FileNotFoundException; +import java.io.RandomAccessFile; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author navid + */ +public class MainforLocks { + + public static void main(String args[]){ + try { + RandomAccessFile file = new RandomAccessFile("/home/navid/randomwords.text", "rw"); + benchmark.init(); + long starttime = System.nanoTime(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread1 = new lockthread1(file, 'a'); + // benchmark.filelock.writeLock().unlock(); + +// benchmark.filelock.writeLock().lock(); + lockthread1 thread2 = new lockthread1(file, 'b'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread3 = new lockthread1(file, 'c'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread4 = new lockthread1(file, 'd'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread5 = new lockthread1(file, 'e'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread6 = new lockthread1(file, 'f'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread7 = new lockthread1(file, 'g'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread8 = new lockthread1(file, 'h'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread9 = new lockthread1(file, 'i'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread10 = new lockthread1(file, 'j'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread11 = new lockthread1(file, 'k'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread12 = new lockthread1(file, 'l'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread13 = new lockthread1(file, 'm'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread14 = new lockthread1(file, 'n'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread15 = new lockthread1(file, 'o'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread16 = new lockthread1(file, 'p'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread17 = new lockthread1(file, 'q'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread18 = new lockthread1(file, 'r'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread19 = new lockthread1(file, 's'); + // benchmark.filelock.writeLock().unlock(); + + // benchmark.filelock.writeLock().lock(); + lockthread1 thread20 = new lockthread1(file, 't'); + // benchmark.filelock.writeLock().unlock(); + + thread1.join(); + thread2.join(); + thread3.join(); + thread4.join(); + thread5.join(); + thread6.join(); + thread7.join(); + thread8.join(); + thread9.join(); + thread10.join(); + thread11.join(); + thread12.join(); + thread13.join(); + thread14.join(); + thread15.join(); + thread16.join(); + thread17.join(); + thread18.join(); + thread19.join(); + thread20.join(); + + long endttime = System.nanoTime(); + System.out.println(endttime - starttime); + System.out.println((endttime - starttime) / 1000000); + int index =97; + for (int j = 0; j < 26; j++) { + ((TransactionalFile)(benchmark.m.get(String.valueOf((char) (index+j))))).close(); + } + } catch (InterruptedException ex) { + Logger.getLogger(MainforLocks.class.getName()).log(Level.SEVERE, null, ex); + } catch (FileNotFoundException ex) { + Logger.getLogger(MainforLocks.class.getName()).log(Level.SEVERE, null, ex); + } + + } +} diff --git a/Robust/Transactions/TransactionalIOSrc/benchmarks/StatisticsWrapper.java b/Robust/Transactions/TransactionalIOSrc/benchmarks/StatisticsWrapper.java new file mode 100644 index 00000000..5fcf21cd --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/benchmarks/StatisticsWrapper.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.benchmarks; + +/** + * + * @author navid + */ +public class StatisticsWrapper { + + public static int totalcommitted; + public static int totalaborted; + +} diff --git a/Robust/Transactions/TransactionalIOSrc/benchmarks/benchmark.java b/Robust/Transactions/TransactionalIOSrc/benchmarks/benchmark.java new file mode 100644 index 00000000..b611e141 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/benchmarks/benchmark.java @@ -0,0 +1,270 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.benchmarks; + +import TransactionalIO.core.Defaults; +import TransactionalIO.core.TransactionalFile; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Random; +import java.util.Vector; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.logging.Level; +import java.util.logging.Logger; + + +/** + * + * @author navid + */ +public class benchmark { + static public HashMap TransactionalFiles = new HashMap(); + static public HashMap hotwords = new HashMap(); + static public HashMap names = new HashMap(); + static public HashMap reversenames = new HashMap(); + static int count = 0; + public static String msg = new String(); + public static ReentrantLock lock = new ReentrantLock(); + public static Vector transacctions = new Vector(); + public static ReentrantLock filelock = new ReentrantLock(); + public static Map m; + public static Map m2; + public static Map m3; + public static Map m4; + public static String[] stocks; + + + public benchmark() { + + } + + private static void preparenamelist(){ + try { + byte[] data = new byte[1]; + char[] name = new char[20]; + RandomAccessFile file = new RandomAccessFile("/home/navid/namelist.text", "rw"); + RandomAccessFile file2 = new RandomAccessFile("/home/navid/financialtransaction.text", "rw"); + RandomAccessFile file3 = new RandomAccessFile("/home/navid/accountbalance.text", "rw"); + + + stocks = new String[20]; + stocks[0] = "Yahoo"; + stocks[1] = "Google"; + stocks[2] = "Microsoft"; + stocks[3] = "Broadcom"; + stocks[4] = "Sun"; + stocks[5] = "Qualcom"; + stocks[6] = "Intel"; + stocks[7] = "WaMU"; + stocks[8] = "BoA"; + stocks[9] = "IMU"; + stocks[10] = "BMW"; + stocks[11] = "Nokia"; + stocks[12] = "Motorolla"; + stocks[13] = "Samsung"; + stocks[14] = "TMobile"; + stocks[15] = "ATT"; + stocks[16] = "PRops"; + stocks[17] = "Asia"; + stocks[18] = "LOLa"; + stocks[19] = "Brita"; + /* boolean hh = false; + boolean found = false; + while (true) { + + if (found){ + System.out.println(file4.getFilePointer()-1); + file4.seek(file4.getFilePointer()-1); + file4.write(' '); + file4.write(stocks[(int)(Math.random()*10)].getBytes()); + file4.write('\n'); + } + if (hh) + break; + found = false; + data[0] = 'a'; + while (data[0] != '\n') { + int tt =0; + tt = file4.read(data); + found = true; + if (tt == -1) { + hh = true; + break; + } + } + }*/ + + boolean flag = false; + boolean done = false; + int wordcounter = 0; + int counter =0; + while(true){ + if (flag) + break; + if (done){ + // System.out.println("At " + wordcounter + " inserted " +String.copyValueOf(name, 0, counter)); + m3.put(Integer.valueOf(wordcounter), String.copyValueOf(name, 0, counter)); + m4.put(String.copyValueOf(name, 0, counter), Integer.valueOf(wordcounter)); + wordcounter++; + done = false; + } + counter = 0; + data[0] = 'a'; + while (data[0] != '\n') { + int res; + res = file.read(data); + if (res == -1) { + flag = true; + break; + } + //System.out.println((char)data[0]); + if (!(Character.isLetter((char) data[0]))) { + continue; + } + name[counter] = (char)data[0]; + done = true; + counter++; + } + } + + + /* counter = 0; + while (counter <30000) { + int index1 = (int)(Math.random()*50); + int stocktrade = (int)(Math.random()*100); + while (stocktrade == 0) + stocktrade = (int)(Math.random()*100); + int index2 = (int)(Math.random()*50); + while (index2 == index1) + index2 = (int)(Math.random()*50); + //System.out.println(index); + String towrite = (String)m3.get(Integer.valueOf(index1)) + " "; + towrite += String.valueOf(stocktrade) + " "; + towrite += (String)m3.get(Integer.valueOf(index2)) + " "; + towrite += stocks[(int)(Math.random()*20)] + "\n"; + + file2.write(towrite.getBytes()); + // System.out.println(towrite); + counter++; + }*/ + // for (int i=0; i<50*Defaults.FILEFRAGMENTSIZE; i++) + //file3.write(''); + + + for (int i=0; i<50; i++){ + String towrite = (String)m3.get(Integer.valueOf(i)) +"\n"; + for (int j=0; j readers; + private Vector blockreaders; + + public static enum MODE {READ, WRITE, READ_WRITE}; + private MODE accessmode; + + protected BlockDataStructure(INode inode, int blocknumber) { + version = new AtomicInteger(0); + //lock = new ReentrantReadWriteLock(); + blockreaders = new Vector(); + lock = new ReentrantReadWriteLock(); + this.inode = inode; + this.blocknumber = blocknumber; + referncount = 0; + owner = null; + } + + public Vector getReaders() { + return blockreaders; + } + + public void setReaders(Vector readers) { + this.blockreaders = readers; + } + + public ReentrantReadWriteLock getLock() { + return lock; + } + + public void setLock(ReentrantReadWriteLock lock) { + this.lock = lock; + } + + public synchronized ExtendedTransaction getOwner() { + return owner; + } + + public synchronized void setOwner(ExtendedTransaction owner) { + this.owner = owner; + } + + public INode getInode() { + return inode; + } + + public void setInode(INode inode) { + this.inode = inode; + } + + public int getBlocknumber() { + return blocknumber; + } + + public void setBlocknumber(int blocknumber) { + this.blocknumber = blocknumber; + } + + public AtomicInteger getVersion() { + return version; + } + + public void setVersion(AtomicInteger version) { + this.version = version; + } + + public MODE getAccessmode() { + return accessmode; + } + + public void setAccessmode(MODE accessmode) { + this.accessmode = accessmode; + } + + public synchronized int getReferncount() { + return referncount; + } + + public synchronized void setReferncount(int referncount) { + this.referncount = referncount; + } + + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/CustomLock.java b/Robust/Transactions/TransactionalIOSrc/core/CustomLock.java new file mode 100644 index 00000000..265716bf --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/CustomLock.java @@ -0,0 +1,62 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + + +import TransactionalIO.benchmarks.benchmark; +import TransactionalIO.benchmarks.thread1; +import TransactionalIO.interfaces.TransactionalProgram; + +/** + * + * @author navid + */ +public class CustomLock implements Runnable{ + + private static ThreadLocal transactioncontainer = new ThreadLocal(); + private TransactionalProgram ioprogram; + private static ThreadLocal/**/ program = new ThreadLocal(); + private ExtendedTransaction transaction; + public Thread runner; + + + public CustomLock(TransactionalProgram ioprogram) { + this.ioprogram = ioprogram; + transaction = new ExtendedTransaction(); + runner = new Thread(this); + runner.start(); + } + + public static void setTransaction(ExtendedTransaction transaction){ + transactioncontainer.set(transaction); + } + + public static ExtendedTransaction getTransaction(){ + return (ExtendedTransaction) transactioncontainer.get(); + } + + public static void setProgram(TransactionalProgram transaction){ + program.set(transaction); + } + + public static TransactionalProgram getProgram(){ + return (TransactionalProgram) program.get(); + } + + + public void run() { + setTransaction(transaction); + setProgram(ioprogram); + synchronized(benchmark.lock){ + benchmark.transacctions.add(transaction); + } +// System.out.println(Thread.currentThread().getName()); + ioprogram.execute(); + transaction.prepareCommit(); + transaction.commitChanges(); + } + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/CustomThread.java b/Robust/Transactions/TransactionalIOSrc/core/CustomThread.java new file mode 100644 index 00000000..429f34c0 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/CustomThread.java @@ -0,0 +1,113 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +import TransactionalIO.exceptions.AbortedException; +import TransactionalIO.benchmarks.benchmark; +import TransactionalIO.interfaces.TransactionalProgram; +import java.io.IOException; +import java.util.Vector; + +/** + * + * @author navid + */ +public class CustomThread implements Runnable{ + + + private static ThreadLocal transactioncontainer = new ThreadLocal(); + private static ThreadLocal onAbort = new ThreadLocal(); + private TransactionalProgram ioprogram; + private static ThreadLocal/**/ program = new ThreadLocal(); + private ExtendedTransaction transaction; + public Thread runner; + + + + public CustomThread(TransactionalProgram ioprogram) { + this.ioprogram = ioprogram; + runner = new Thread(this); + runner.start(); + } + + + + /* public static void setTransaction(ExtendedTransaction transaction){ + transactioncontainer.set(transaction); + } + + public static ExtendedTransaction getTransaction(){ + return (ExtendedTransaction) transactioncontainer.get(); + }*/ + + public static void setProgram(TransactionalProgram transaction){ + program.set(transaction); + } + + public static TransactionalProgram getProgram(){ + return (TransactionalProgram) program.get(); + } + + public static Vector getonAbort(){ + return (Vector) onAbort.get(); + } + + public static void setonAbort(Vector s){ + onAbort.set(s); + } + + + public void run() { + + setProgram(ioprogram); + // setonAbort(new Vector()); +// System.out.println(Thread.currentThread().getName()); + while (true){ + try{ + /* getonAbort().add(new terminateHandler() { + public void cleanup() { + synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() +" KEWL"); + } + } + }); + */ + + Wrapper.Initialize(null); + // transaction = new ExtendedTransaction(); + // setTransaction(transaction); + synchronized(benchmark.lock){ + benchmark.transacctions.add(Wrapper.getTransaction()); + } + ioprogram.execute(); + Wrapper.prepareIOCommit(); + + Wrapper.commitIO(); + break; + } + catch (AbortedException e){ + /* Iterator it = getonAbort().iterator(); + while(it.hasNext()) { + terminateHandler th = (terminateHandler)it.next(); + th.cleanup(); + } + getonAbort().clear();*/ + + + /* synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() +" retried"); + }*/ + + } + finally{ + Wrapper.getTransaction().unlockAllLocks(); + } + } + } + + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/Defaults.java b/Robust/Transactions/TransactionalIOSrc/core/Defaults.java new file mode 100644 index 00000000..cf8d075e --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/Defaults.java @@ -0,0 +1,73 @@ +package TransactionalIO.core; + +/* + * Defaults.java + * + * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa + * Clara, California 95054, U.S.A. All rights reserved. + * + * Sun Microsystems, Inc. has intellectual property rights relating to + * technology embodied in the product that is described in this + * document. In particular, and without limitation, these + * intellectual property rights may include one or more of the + * U.S. patents listed at http://www.sun.com/patents and one or more + * additional patents or pending patent applications in the U.S. and + * in other countries. + * + * U.S. Government Rights - Commercial software. + * Government users are subject to the Sun Microsystems, Inc. standard + * license agreement and applicable provisions of the FAR and its + * supplements. Use is subject to license terms. Sun, Sun + * Microsystems, the Sun logo and Java are trademarks or registered + * trademarks of Sun Microsystems, Inc. in the U.S. and other + * countries. + * + * This product is covered and controlled by U.S. Export Control laws + * and may be subject to the export or import laws in other countries. + * Nuclear, missile, chemical biological weapons or nuclear maritime + * end uses or end users, whether direct or indirect, are strictly + * prohibited. Export or reexport to countries subject to + * U.S. embargo or to entities identified on U.S. export exclusion + * lists, including, but not limited to, the denied persons and + * specially designated nationals lists is strictly prohibited. + */ + + + +/** + * + * @author Maurice Herlihy + */ + + +public class Defaults { + /** + * how many threads + **/ + public static final int THREADS = 1; + /** + * benchmark duration in milliseconds + **/ + public static final int TIME = 10000; + /** + * uninterpreted arg passed to benchmark + **/ + public static final int EXPERIMENT = 100; + /** + * fully-qualified contention manager name + **/ + public static final String MANAGER = "dstm2.manager.BackoffManager"; + /** + * fully-qualified factory name + **/ + public static final String FACTORY = "dstm2.factory.shadow.Factory"; + /** + * fully-qualified adapter name + **/ + public static final String ADAPTER = "dstm2.factory.shadow.Adapter";; + + public static final int FILEFRAGMENTSIZE= 1024; + + public static final boolean READWRITECONFLIT = true; + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/ExtendedTransaction.java b/Robust/Transactions/TransactionalIOSrc/core/ExtendedTransaction.java new file mode 100644 index 00000000..42b757ab --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/ExtendedTransaction.java @@ -0,0 +1,971 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.core; + + + + + +import TransactionalIO.exceptions.AbortedException; +import TransactionalIO.benchmarks.benchmark; +import TransactionalIO.benchmarks.customhandler; +import TransactionalIO.benchmarks.customhandler; +import TransactionalIO.interfaces.BlockAccessModesEnum; +import TransactionalIO.interfaces.ContentionManager; +import TransactionalIO.interfaces.TransactionStatu; +//import dstm2.file.managers.BaseManager; +import java.awt.event.ActionListener; +import java.beans.EventHandler; +import java.beans.PropertyChangeEvent; +import java.beans.PropertyChangeListener; +import java.beans.PropertyChangeSupport; +import java.io.FileDescriptor; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.util.Collections; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.TreeMap; +import java.util.Vector; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author navid + */ +public class ExtendedTransaction implements TransactionStatu { + + + private native int nativepwrite(byte buff[], long offset, int size, FileDescriptor fd); + + { + System.load("/home/navid/libkooni.so"); + } + + private boolean flag = true; + public TransactionStatu memorystate; + private PropertyChangeSupport changes = new PropertyChangeSupport(this); + public int starttime; + public int endtime; + + public TreeMap msg = new TreeMap(); + public int numberofwrites; + public int numberofreads; + + public enum Status {ABORTED, ACTIVE, COMMITTED}; + private boolean writesmerged = true; + + //private Vector heldoffsetlocks; + private Vector heldoffsetlocks; + + //private Vector heldblocklocks; + private Vector heldblocklocks; + + //private HashMap> AccessedFiles; + private HashMap AccessedFiles; + + + //private HashMap > accessedBlocks; + private HashMap accessedBlocks; + + //private HashMap LocaltoGlobalMappings; + private HashMap GlobaltoLocalMappings; + + public HashMap merge_for_writes_done; + + + + + private HashMap writeBuffer; + + private ContentionManager contentionmanager; + private /*volatile*/ Status status; + + private int id; + + + + public ExtendedTransaction() { + // super(); + // id = Integer.valueOf(Thread.currentThread().getName().substring(7)); + heldblocklocks = new Vector() ; + heldoffsetlocks= new Vector(); + AccessedFiles = new HashMap(); + GlobaltoLocalMappings = new HashMap/**/(); + writeBuffer = new HashMap(); + status = Status.ACTIVE; + accessedBlocks = new HashMap(); + merge_for_writes_done = new HashMap(); + writesmerged = true; + // setContentionmanager(new BaseManager()); + // beginTransaction(); + + } + + public ExtendedTransaction(TransactionStatu memorystate){ + this(); + /* heldblocklocks = new Vector() ; + heldoffsetlocks= new Vector(); + AccessedFiles = new HashMap(); + GlobaltoLocalMappings = new HashMap(); + writeBuffer = new HashMap(); + status = Status.ACTIVE; + accessedBlocks = new HashMap(); + merge_for_writes_done = new HashMap(); + writesmerged = true;*/ + this.memorystate = memorystate ; + } + + private int invokeNativepwrite(byte buff[], long offset, int size, RandomAccessFile file) { + try { + //System.out.println(buff.length); + // System.out.println(offset); + return nativepwrite(buff, offset, buff.length, file.getFD()); + } catch (IOException ex) { + + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } + + } + + public void beginTransaction(){ + this.addPropertyChangeListener(new customhandler(Status.ABORTED)); + } + + + + + public void abort() { + synchronized(this){ + // Status oldst = getStatus(); + /* synchronized(benchmark.lock){ + System.out.println("be ga raftim 0"); + }*/ + this.status = Status.ABORTED; + if (this.memorystate !=null && !(this.memorystate).isAborted()){ + /* synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() +" be ga raftim 1 file"); + }*/ + this.memorystate.abortThisSystem(); + /* synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() + " be ga raftim 2 file"); + }*/ + } + // Thread[] group = new Thread[30]; + // Thread.currentThread().enumerate(group); + // group[this.id].interrupt(); + /*synchronized(benchmark.lock){ + System.out.println("/////////////"); + System.out.println(Thread.currentThread() + " " +Thread.currentThread().enumerate(group)); + System.out.println(Thread.currentThread() + " " +group[0]); + System.out.println(Thread.currentThread() + " " +group[1]); + System.out.println(Thread.currentThread() + " " +group[2]); + System.out.println("/////////////"); + }*/ + + + // this.changes.firePropertyChange("status", oldst, Status.ABORTED); + } + + } + + public Status getStatus() { + return status; + } + + public boolean isActive() { + return this.getStatus() == Status.ACTIVE; + } + + + public boolean isAborted() { + return this.getStatus() == Status.ABORTED; + } + + public ContentionManager getContentionmanager() { + return contentionmanager; + } + + public void setContentionmanager(ContentionManager contentionmanager) { + this.contentionmanager = contentionmanager; + } + + + public HashMap getWriteBuffer() { + return writeBuffer; + } + + public HashMap getAccessedFiles() { + return AccessedFiles; + } + + public boolean isWritesmerged() { + return writesmerged; + } + + public void setWritesmerged(boolean writesmerged) { + this.writesmerged = writesmerged; + } + + + + + + + public HashMap getGlobaltoLocalMappings() { + return GlobaltoLocalMappings; + } + + public HashMap getAccessedBlocks() { + return accessedBlocks; + } + + + public ContentionManager getBlockContentionManager(){ + return ManagerRepository.getBlockcm(); + } + + public ContentionManager getOffsetContentionManager(){ + return ManagerRepository.getOffsetcm(); + } + + public TreeMap getSortedFileAccessMap(HashMap hmap) { + /*TreeMap sortedMap = new TreeMap(hmap); + return sortedMap;*/ + return new TreeMap(hmap); + } + + + public void setStatus(Status st){ + Status oldst = getStatus(); + this.status = st; + this.changes.firePropertyChange("status", oldst, st); + } + + + + + public void addFile(TransactionalFile tf/*, TransactionLocalFileAttributes tmp*/) { + + + /* if (tf.appendmode) { + this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.APPEND); + } else if (tf.writemode) { + this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.READ_WRITE); + } else { + this.addtoFileAccessModeMap(tf.getInode(), FileAccessModesEum.READ); + }*/ + // System.out.println("dsadssasadssa"); + + tf.lockOffset(this); + + //tf.offsetlock.lock(); + TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(tf.getCommitedoffset().getOffsetnumber()/*, tf.getInodestate().commitedfilesize.get()*/); + //this.heldoffsetlocks.remove(tf.offsetlock); + tf.offsetlock.unlock(); + + Vector dummy; + + if (AccessedFiles.containsKey(tf.getInode())){ + dummy = (Vector) AccessedFiles.get(tf.getInode()); + } + else{ + dummy = new Vector(); + AccessedFiles.put(tf.getInode(), dummy); + } + + + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Unlocked the offset lock " + tf.offsetlock + " for file " + tf.getInode() + " form descriptor " + tf.getSequenceNum()); + dummy.add(tf); + GlobaltoLocalMappings.put(tf, tmp); + merge_for_writes_done.put(tf.getInode(), Boolean.TRUE); + + + + //} + + } + + + public boolean lockOffsets() { /// Locking offsets for File Descriptors + + + TreeMap hm = getSortedFileAccessMap(AccessedFiles); + Iterator iter = hm.keySet().iterator(); + + while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) { + INode key = (INode) iter.next(); + + Vector vec = (Vector) AccessedFiles.get(key); + Collections.sort(vec); + Iterator it = vec.iterator(); + while (it.hasNext()){ + TransactionalFile value = (TransactionalFile) it.next(); + while (this.getStatus() ==Status.ACTIVE){ + //if (value.offsetlock.tryLock()) { + value.offsetlock.lock(); + + // synchronized(value.getCommitedoffset()){ + // value.getCommitedoffset().setOffsetOwner(this); + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the offset lock in commit for file " + value.getInode() + " from descriptor "+ value.getSequenceNum() +"\n"); + heldoffsetlocks.add(value.offsetlock); + + //else + // getContentionmanager().resolveConflict(this, value.getCommitedoffset()); + break; + //} + } + if (this.getStatus() != Status.ACTIVE){ + + + return false; + } + } + // outercounter++; + } + if (this.getStatus() != Status.ACTIVE){ + + + return false; + } + return true; + } + + /*public boolean commit() { /// Locking offsets for File Descriptors + + Map hm = getSortedFileAccessMap(FilesAccesses); + //lock phase + Iterator iter = hm.keySet().iterator(); + TransactionLocalFileAttributes value; + while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) { + INode key = (INode) iter.next(); + value = (TransactionLocalFileAttributes) hm.get(key); + synchronized(value.getCurrentcommitedoffset()){ + if (value.offsetlock.tryLock()) { + value.getCurrentcommitedoffset().setOffsetOwner(this); + heldblocklocks.add(value.offsetlock); + Iterator it = value.getCurrentcommitedoffset().getOffsetReaders().iterator(); // for in-place aborting visible readers strategy + while (it.hasNext()) + { + ExtendedTransaction tr = (ExtendedTransaction) it.next(); + tr.abort(); + } + } + } + } + getOffsetContentionManager().resolveConflict(this, value.getCurrentcommitedoffset().getOffsetOwner()); + } + return true; + } */ + + /*public boolean commit() { /// Locking offsets for File Descriptors with checking strategy + + Map hm = getSortedFileAccessMap(FilesAccesses); + //lock phase + Iterator iter = hm.keySet().iterator(); + TransactionLocalFileAttributes value; + while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) { + INode key = (INode) iter.next(); + value = (TransactionLocalFileAttributes) hm.get(key); + + if (value.isValidatelocaloffset()) { + if (value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber()) { + value.offsetlock.lock(); + heldoffsetlocks.add(value.offsetlock); + if (!(value.getCopylocaloffset() == value.currentcommitedoffset.getOffsetnumber())) { + unlockAllLocks(); + return false; + } + } else { + unlockAllLocks(); + return false; + } + } else { + value.offsetlock.lock(); + heldoffsetlocks.add(value.offsetlock); + } + } + }*/ + + + + + public boolean lockBlock(BlockDataStructure block, BlockAccessModesEnum mode/*, GlobalINodeState adapter, BlockAccessModesEnum mode, int expvalue, INode inode, TransactionLocalFileAttributes tf*/) { + + + //boolean locked = false; + Lock lock; + + + + if (mode == BlockAccessModesEnum.READ){ + lock = block.getLock().readLock(); + + + } + else { + + lock = block.getLock().writeLock(); + + } + + while (this.getStatus() == Status.ACTIVE) { + //synchronized(block){ + + // if (lock.tryLock()) { + lock.lock(); + // synchronized(benchmark.lock){ + // System.out.println(Thread.currentThread() + " Lock the block lock for " + lock +" number " + block.getBlocknumber()); + // } + heldblocklocks.add(lock); + // block.setOwner(this); + return true; + // } + + + //getContentionmanager().resolveConflict(this, block); + } + + return false; + } + /* + public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // from here for visible readers strategy + while (this.getStatus() == Status.ACTIVE) { + if (lock.tryLock()) { + Thread.onAbortOnce(new Runnable() { + + public void run() { + lock.unlock(); + } + }); + + heldblocklocks.add(lock); + + synchronized (adapter) { + block.setOwner(this); + // Iterator it = block.getReaders().iterator(); + // while (it.hasNext()) + // { + // ExtendedTransaction tr = (ExtendedTransaction) it.next(); + // tr.abort(); + // } + } + + return true; + } else { + getBlockContentionManager().resolveConflict(this, block.getOwner()); + } + } + return false;*/ + + + /* + public boolean lockBlock(BlockDataStructure block, Adapter adapter, BlockAccessModesEnum mode, int expvalue) { // versioning strat + while (this.getStatus() == Status.ACTIVE) { + if (lock.tryLock()) { + Thread.onAbortOnce(new Runnable() { + + public void run() { + lock.unlock(); + } + }); + + heldblocklocks.add(lock); + if (mode != BlockAccessModesEnum.WRITE) { egy + if (block.getVersion().get() != expvalue) { + unlockAllLocks(); + return false; + } + } + synchronized (adapter) { + block.setOwner(this); + } + + return true; + } else { + getContentionManager().resolveConflict(this, block.getOwner()); + } + } + return false; + }*/ + + public void prepareCommit() { + if (this.status != Status.ACTIVE) + throw new AbortedException(); + + boolean ok = true; + if (!lockOffsets()) + { +// unlockAllLocks(); + // this.msg.put(System.nanoTime(),Thread.currentThread().getName() + " Aborted \n"); + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Aborted in prepare commit\n"; + }*/ + //Thread.currentThread().stop(); + throw new AbortedException(); + } + + + /////////////////////////// + + + Map hm = getWriteBuffer(); + + Iterator iter = hm.keySet().iterator(); + WriteOperations value; + Vector vec = new Vector(); + while (iter.hasNext() && (this.getStatus() == Status.ACTIVE) && ok) { + //int expvalue = 0; + + INode key = (INode) iter.next(); + vec = (Vector) hm.get(key); + Collections.sort(vec); + Iterator it = vec.iterator(); + while (it.hasNext()){ + + value = (WriteOperations) it.next(); + if (value.isUnknownoffset()){ + + long start; + long end; + + //synchronized(value.getOwnertransactionalFile().getCommitedoffset()){ + start = value.getRange().getStart() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber(); + end = value.getRange().getEnd() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber(); + if (value.getBelongingto().isUnknown_inital_offset_for_write()){ + value.getBelongingto().setLocaloffset(value.getBelongingto().getLocaloffset() - value.getBelongingto().getCopylocaloffset() + value.getOwnertransactionalFile().getCommitedoffset().getOffsetnumber()); + value.getBelongingto().setUnknown_inital_offset_for_write(false); + } + + //} + // System.out.println("start write " + start); + /// System.out.println("end write " + end); + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(start); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(start, value.getRange().getEnd() - value.getRange().getStart()); + + TreeMap sset; + if (this.getAccessedBlocks().get(key) != null){ + sset = (TreeMap) this.getAccessedBlocks().get(key); + } + + else{ + sset = new TreeMap(); + this.getAccessedBlocks().put(key, sset); + } + + + for (int i = startblock; i <= targetblock; i++) { + if (sset.containsKey(Integer.valueOf(i))){ + if (sset.get(Integer.valueOf(i)) != BlockAccessModesEnum.WRITE) + sset.put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE); + } + else + sset.put(Integer.valueOf(i), BlockAccessModesEnum.WRITE); + + // tt.add(Integer.valueOf(i)); + } + + value.getRange().setStart(start); + value.getRange().setEnd(end); + + // System.out.println(Thread.currentThread().); + // System.out.println(value.getRange().getStart()); + // System.out.println(value.getRange().getEnd()); + // System.out.println("---------------"); + //this.getAccessedBlocks().put(value.getOwnertransactionalFile().getInode(), sset); + } + } + + } + + Iterator it = this.getAccessedBlocks().keySet().iterator(); + while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) { + INode inode = (INode) it.next(); + GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(inode); + TreeMap vec2 = (TreeMap) this.getAccessedBlocks().get(inode); + Iterator iter2 = vec2.keySet().iterator(); + while(iter2.hasNext()){ + Integer num = (Integer) iter2.next(); + + //BlockDataStructure blockobj = (BlockDataStructure) inodestate.lockmap.get(num); + BlockDataStructure blockobj; + // if (((BlockAccessModesEnum)vec2.get(num)) == BlockAccessModesEnum.WRITE){ + blockobj = inodestate.getBlockDataStructure(num); + // } + // else + // blockobj = (BlockDataStructure) inodestate.lockmap.get(num); + + ok = this.lockBlock(blockobj, (BlockAccessModesEnum)vec2.get(num)); + if (ok == false) + break; + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() +" for " + inode + "\n"; + }*/ + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Locked the Block Number " + blockobj.getBlocknumber() + " for file " + inode + "\n"); + } + } + + if (this.getStatus() != Status.ACTIVE){ + // unlockAllLocks(); + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Aborted \n"); + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Aborted \n"; + }*/ + // Thread.currentThread().stop(); + throw new AbortedException(); + } + abortAllReaders(); + + // } + //expvalue = ((Integer) value.getBlockversions().get(it)).intValue(); //for versioning strategy + /*if (!(value.isValidatelocaloffset())) { + if (((BlockAccessModesEnum) (value.getAccesedblocks().get(blockno))) != BlockAccessModesEnum.WRITE) { //versioning strategy + + /if (blockobj.getVersion().get() == expvalue) { + + ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue); + if (ok == false) { + // unlockAllLocks(); + break; + } + } else { + ok = false; + break; + } + } else { + + ok = this.lock(blockobj, value.adapter, (BlockAccessModesEnum) (value.getAccesedblocks().get(blockno)), expvalue); + if (ok == false) { + break; + } + } + } + + + if (!(ok)) { + unlockAllLocks(); + throw new AbortedException(); + }*/ + } + + public void commitChanges(){ + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " is committing \n"); + + + + //synchronized(benchmark.lock){ + // System.out.println(Thread.currentThread().getName() + " is commiting"); + //} + + + Map hm = getWriteBuffer(); + Iterator iter = hm.keySet().iterator(); + Iterator it; + WriteOperations writeop; + Vector vec; + while (iter.hasNext() && (this.getStatus() == Status.ACTIVE)) { + INode key = (INode) iter.next(); + + vec = (Vector) hm.get(key); + Collections.sort(vec); + it = vec.iterator(); + while (it.hasNext()){ + + + //value = (WriteOperations) it.next(); + // writeop = (WriteOperations) writeBuffer.get(key); + writeop = (WriteOperations) it.next(); + // System.out.println(writeop); + Byte[] data = new Byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())]; + byte[] bytedata = new byte[(int) (writeop.getRange().getEnd() - writeop.getRange().getStart())]; + data = (Byte[]) writeop.getData(); + + for (int i = 0; i < data.length; i++) { + bytedata[i] = data[i]; + } + + // try { + // + // writeop.getOwnertransactionalFile().file.seek(writeop.getRange().getStart()); + // System.out.println(Thread.currentThread() + " range " + writeop.getRange().getStart()); + // writeop.getOwnertransactionalFile().file.write(bytedata); + invokeNativepwrite(bytedata, writeop.getRange().getStart(), bytedata.length, writeop.getOwnertransactionalFile().file); + // System.out.println(Thread.currentThread() + " " + bytedata); + + // } catch (IOException ex) { + // Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex); + // } + // + + } + } + + /*if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.APPEND) { + try { + Range range = (Range) value.getWrittendata().firstKey(); + + + //synchronized(value.adapter){ + //value.f.seek(value.adapter.commitedfilesize.get()); + value.f.seek(value.getFilelength()); + //} + + Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())]; + byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())]; + data = (Byte[]) value.getWrittendata().get(range); + + for (int i = 0; i < data.length; i++) { + bytedata[i] = data[i]; + } + value.f.write(bytedata); + + } catch (IOException ex) { + Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex); + } + + } else if (((FileAccessModesEum) (this.FilesAccessModes.get(key))) == FileAccessModesEum.READ) { + continue; + } + else if (value.relocatablewrite && value.getContinious_written_data() != null){ + + + } + else if (!(value.getNon_Speculative_Writtendata().isEmpty())) { + int tobeaddedoffset = 0; + + if (value.isValidatelocaloffset()) { + tobeaddedoffset = 0; + } else { + tobeaddedoffset = (int) (value.getCurrentcommitedoffset().getOffsetnumber() - value.getCopylocaloffset()); + } + Iterator it = value.getNon_Speculative_Writtendata().keySet().iterator(); + int counter = 0; + while (it.hasNext() && (this.getStatus() == Status.ACTIVE)) { + try { + Range range = (Range) it.next(); + + + value.f.seek(range.getStart() + tobeaddedoffset); + + Byte[] data = new Byte[(int) (range.getEnd() - range.getStart())]; + byte[] bytedata = new byte[(int) (range.getEnd() - range.getStart())]; + data = (Byte[]) value.getNon_Speculative_Writtendata().get(range); + + for (int i = 0; i < data.length; i++) { + bytedata[i] = data[i]; + } + value.f.write(bytedata); + counter++; + + } catch (IOException ex) { + Logger.getLogger(ExtendedTransaction.class.getName()).log(Level.SEVERE, null, ex); + } + } + } else { + continue; + } + } + + + iter = hm.keySet().iterator(); + while (iter.hasNext() ) { + INode key = (INode) iter.next(); + value = (TransactionLocalFileAttributes) hm.get(key); + Iterator it = value.getAccesedblocks().keySet().iterator(); + + while (it.hasNext()) { + Integer blockno = (Integer) it.next(); + synchronized (value.adapter) { + //BlockDataStructure blockobj = (BlockDataStructure) value.adapter.lockmap.get(blockno); + //blockobj.getVersion().getAndIncrement(); for versioning strategy + //value.getCurrentcommitedoffset().setOffsetnumber(value.getLocaloffset()); + //value.adapter.commitedfilesize.getAndSet(value.getFilelength()); + } + } + }*/ + Iterator k = GlobaltoLocalMappings.keySet().iterator(); + while (k.hasNext()){ + TransactionalFile trf = (TransactionalFile) (k.next()); + // synchronized(trf.getCommitedoffset()){ + trf.getCommitedoffset().setOffsetnumber(((TransactionLocalFileAttributes)GlobaltoLocalMappings.get(trf)).getLocaloffset()); + /*synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() + " KIRIR " +GlobaltoLocalMappings.get(trf).getLocaloffset()); + }*/ + // } + } + //unlockAllLocks(); + + } + + public void unlockAllLocks() { + Iterator it = heldblocklocks.iterator(); + + while (it.hasNext()) { + + Lock lock = (Lock) it.next(); + lock.unlock(); + + /*synchronized(benchmark.lock){ + System.out.println(Thread.currentThread().getName() + " Released the block lock for " + lock); + }*/ + } + heldblocklocks.clear(); + + it = heldoffsetlocks.iterator(); + while (it.hasNext()) { + ReentrantLock lock = (ReentrantLock) it.next(); + lock.unlock(); + // synchronized(benchmark.lock){ + // System.out.println(Thread.currentThread().getName() + " Released the offset lock for "+ lock +"\n"); + // } + } + heldoffsetlocks.clear(); + } + + public void abortAllReaders(){ + TreeMap hm = getSortedFileAccessMap(AccessedFiles); + //lock phase + Iterator iter = hm.keySet().iterator(); + TransactionalFile value; + while (iter.hasNext()) { + INode key = (INode) iter.next(); + Vector vec = (Vector) AccessedFiles.get(key); + Iterator it = vec.iterator(); + while (it.hasNext()) + { + + value = (TransactionalFile)it.next(); + + //value = (TransactionalFile) hm.get(key); + //System.out.println(value.getCommitedoffset().getOffsetReaders()); + + Iterator it2 = value.getCommitedoffset().getOffsetReaders().iterator(); // for visible readers strategy + while ( it2.hasNext()) + { + + ExtendedTransaction tr = (ExtendedTransaction) it2.next(); + if (tr != this) + tr.abort(); + } + value.getCommitedoffset().getOffsetReaders().clear(); + //} + } + + + + TreeMap vec2; + if (accessedBlocks.get(key) != null){ + vec2 = (TreeMap) accessedBlocks.get(key); + } + else{ + vec2 = new TreeMap(); + + } + GlobalINodeState inodestate = TransactionalFileWrapperFactory.getTateransactionalFileINodeState(key); + Iterator it2 = vec2.keySet().iterator(); + + while (it2.hasNext()) + { + + Integer num = (Integer)it2.next(); + if (vec2.get(num) != BlockAccessModesEnum.READ) + { + BlockDataStructure blockobj = (BlockDataStructure) inodestate.getBlockDataStructure(num);//lockmap.get(num); + Iterator it4 = blockobj.getReaders().iterator(); // from here for visible readers strategy + + while (it4.hasNext()) + { + + ExtendedTransaction tr = (ExtendedTransaction) it4.next(); + if (this != tr) + tr.abort(); + } + blockobj.getReaders().clear(); + + } + } + + + + + /* SortedSet sst = (SortedSet) this.getAccessedBlocks().get(key); + Iterator it3 = sst.iterator(); + while (it3.hasNext()){ + Integer num = (Integer)it.next(); + BlockDataStructure blockobj = (BlockDataStructure) value.getInodestate().lockmap.get(num); + Iterator it4 = blockobj.getReaders().iterator(); // from here for visible readers strategy + while (it4.hasNext()) + { + ExtendedTransaction tr = (ExtendedTransaction) it3.next(); + tr.abort(); + } + + }*/ + + } + } + + public void addPropertyChangeListener(PropertyChangeListener listener){ + this.changes.addPropertyChangeListener("status",listener); + } + + public void removePropertyChangeListener(PropertyChangeListener listener){ + this.changes.removePropertyChangeListener("status",listener); + } + + public TransactionStatu getOtherSystem() { + return memorystate; + } + + public void setOtherSystem(TransactionStatu othersystem) { + memorystate = othersystem; + } + + public Vector getHeldblocklocks() { + return heldblocklocks; + } + + public void setHeldblocklocks(Vector heldblocklocks) { + this.heldblocklocks = heldblocklocks; + } + + public Vector getHeldoffsetlocks() { + return heldoffsetlocks; + } + + public void setHeldoffsetlocks(Vector heldoffsetlocks) { + this.heldoffsetlocks = heldoffsetlocks; + } + + public void abortThisSystem() { + abort(); + } + + public boolean isCommitted() { + if (this.status == Status.COMMITTED) + return true; + + return false; + + } + + + +} + + + + + + diff --git a/Robust/Transactions/TransactionalIOSrc/core/FileBlockManager.java b/Robust/Transactions/TransactionalIOSrc/core/FileBlockManager.java new file mode 100644 index 00000000..bff7234d --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/FileBlockManager.java @@ -0,0 +1,34 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.core; + +//import Defaults; + + + + + + + + + +/** + * + * @author navid + */ +public class FileBlockManager { + + public static long getFragmentIndexofTheFile(long filesize) { + return (filesize / Defaults.FILEFRAGMENTSIZE); + } + + public static int getCurrentFragmentIndexofTheFile(long start) { + return (int) ((start / Defaults.FILEFRAGMENTSIZE)); + } + + public static int getTargetFragmentIndexofTheFile(long start, long offset) { + return (int) (((offset + start) / Defaults.FILEFRAGMENTSIZE)); + } +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/GlobalINodeState.java b/Robust/Transactions/TransactionalIOSrc/core/GlobalINodeState.java new file mode 100644 index 00000000..f3705a9e --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/GlobalINodeState.java @@ -0,0 +1,118 @@ +/* + * Adapter.java + * + * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa + * Clara, California 95054, U.S.A. All rights reserved. + * + * Sun Microsystems, Inc. has intellectual property rights relating to + * technology embodied in the product that is described in this + * document. In particular, and without limitation, these + * intellectual property rights may include one or more of the + * U.S. patents listed at http://www.sun.com/patents and one or more + * additional patents or pending patent applications in the U.S. and + * in other countries. + * + * U.S. Government Rights - Commercial software. + * Government users are subject to the Sun Microsystems, Inc. standard + * license agreement and applicable provisions of the FAR and its + * supplements. Use is subject to license terms. Sun, Sun + * Microsystems, the Sun logo and Java are trademarks or registered + * trademarks of Sun Microsystems, Inc. in the U.S. and other + * countries. + * + * This product is covered and controlled by U.S. Export Control laws + * and may be subject to the export or import laws in other countries. + * Nuclear, missile, chemical biological weapons or nuclear maritime + * end uses or end users, whether direct or indirect, are strictly + * prohibited. Export or reexport to countries subject to + * U.S. embargo or to entities identified on U.S. export exclusion + * lists, including, but not limited to, the denied persons and + * specially designated nationals lists is strictly prohibited. + */ +package TransactionalIO.core; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Obstruction-free atomic object implementation. Visible reads. + * Support snapshots and early release. + * @author Navid Farri + */ +public class GlobalINodeState { + + // public HashMap lockmap; + public HashMap lockmap; + private ConcurrentHashMap conlockmap = new ConcurrentHashMap(); + + public AtomicLong commitedfilesize = new AtomicLong(); + private ExtendedTransaction writer; + public int seqNum = 0; + private INode inode; + + public GlobalINodeState() { + } + + + + + protected GlobalINodeState(INode inode, long length) { + writer = null; + lockmap = new HashMap(); + + commitedfilesize.set(length); + this.inode = inode; + } + + + + + public AtomicLong getCommitedfilesize() { + return commitedfilesize; + } + + public void setCommitedfilesize(AtomicLong commitedfilesize) { + this.commitedfilesize = commitedfilesize; + } + + public ExtendedTransaction getWriter() { + return writer; + } + + public void setWriter(ExtendedTransaction writer) { + this.writer = writer; + } + + public BlockDataStructure getBlockDataStructure(Integer blocknumber) { + /* synchronized (lockmap) { + if (lockmap.containsKey(blocknumber)) { + return ((BlockDataStructure) (lockmap.get(blocknumber))); + } else { + BlockDataStructure tmp = new BlockDataStructure(inode, blocknumber); + lockmap.put(blocknumber, tmp); + return tmp; + } + } + }*/ + BlockDataStructure rec = (BlockDataStructure)conlockmap.get(blocknumber); + if (rec == null) { + // record does not yet exist + BlockDataStructure newRec = new BlockDataStructure(inode, blocknumber); + rec = (BlockDataStructure)conlockmap.putIfAbsent(blocknumber, newRec); + if (rec == null) { + // put succeeded, use new value + rec = newRec; + } + } + return rec; +} + + + + + +} + diff --git a/Robust/Transactions/TransactionalIOSrc/core/GlobalOffset.java b/Robust/Transactions/TransactionalIOSrc/core/GlobalOffset.java new file mode 100644 index 00000000..f32ffe3b --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/GlobalOffset.java @@ -0,0 +1,52 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +import java.util.Vector; + +/** + * + * @author navid + */ +public class GlobalOffset { + private long offsetnumber; + //private Vector offsetReaders; + private Vector offsetReaders = new Vector(); + private ExtendedTransaction offsetOwner; + + public GlobalOffset(long offsetnumber) { + this.offsetnumber = offsetnumber; + } + + + public long getOffsetnumber() { + return offsetnumber; + } + + public ExtendedTransaction getOffsetOwner(){ + return offsetOwner; + } + + public void setOffsetOwner(ExtendedTransaction ex){ + offsetOwner = ex; + } + + public void setOffsetnumber(long offsetnumber) { + this.offsetnumber = offsetnumber; + } + + public Vector getOffsetReaders() { + return offsetReaders; + } + + public void setOffsetReaders(Vector offsetReaders) { + this.offsetReaders = offsetReaders; + } + + + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.c b/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.c new file mode 100644 index 00000000..35fa4e02 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.c @@ -0,0 +1,9 @@ +#include +#include +#include "HelloWorld.h" + +JNIEXPORT void JNICALL Java_HelloWorld_displayMessage(JNIEnv *env, jobject obj){ + printf("HelloWorld!\n"); +} + + diff --git a/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.h b/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.h new file mode 100644 index 00000000..7cdd58e3 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/HelloWorld.h @@ -0,0 +1,19 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ + +#include + +#ifndef __dstm2_file_factory_HelloWorld__ +#define __dstm2_file_factory_HelloWorld__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +JNIEXPORT void JNICALL Java_dstm2_file_factory_HelloWorld_displayMessage (JNIEnv *env, jobject); + +#ifdef __cplusplus +} +#endif + +#endif /* __dstm2_file_factory_HelloWorld__ */ diff --git a/Robust/Transactions/TransactionalIOSrc/core/INode.java b/Robust/Transactions/TransactionalIOSrc/core/INode.java new file mode 100644 index 00000000..e5a1f826 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/INode.java @@ -0,0 +1,61 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +/** + * + * @author navid + */ +public class INode implements Comparable{ + + private String filepath; + private long number; + + + public INode(long number) { + this.number = number; + } + + public INode(long number, String filepath) { + this(number); + this.filepath = filepath; + } + + public String getFilepath() { + return filepath; + } + + public void setFilepath(String filepath) { + this.filepath = filepath; + } + + + + public long getNumber() { + return number; + } + + public void setNumber(long number) { + this.number = number; + } + + public int compareTo(Object arg0) { + INode other = (INode) arg0; + if (this.getNumber() < other.getNumber()) + return -1; + else if (this.getNumber() > other.getNumber()) + return 1; + else{ + System.out.println("Logical Eroor Two Inodes cannot have the same number"); + return 0; + } + + } + + + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/IOlib b/Robust/Transactions/TransactionalIOSrc/core/IOlib new file mode 100644 index 00000000..cc5c9907 Binary files /dev/null and b/Robust/Transactions/TransactionalIOSrc/core/IOlib differ diff --git a/Robust/Transactions/TransactionalIOSrc/core/IOlib.so b/Robust/Transactions/TransactionalIOSrc/core/IOlib.so new file mode 100644 index 00000000..cc5c9907 Binary files /dev/null and b/Robust/Transactions/TransactionalIOSrc/core/IOlib.so differ diff --git a/Robust/Transactions/TransactionalIOSrc/core/ManagerRepository.java b/Robust/Transactions/TransactionalIOSrc/core/ManagerRepository.java new file mode 100644 index 00000000..79e2efd9 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/ManagerRepository.java @@ -0,0 +1,36 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +import TransactionalIO.interfaces.ContentionManager; + + +/** + * + * @author navid + */ +public class ManagerRepository { + private static ContentionManager blockcm; + private static ContentionManager offsetcm; + + public synchronized static ContentionManager getBlockcm() { + return blockcm; + } + + public synchronized static void setBlockcm(ContentionManager blockcm) { + ManagerRepository.blockcm = blockcm; + } + + public synchronized static ContentionManager getOffsetcm() { + return offsetcm; + } + + public synchronized static void setOffsetcm(ContentionManager offsetcm) { + ManagerRepository.offsetcm = offsetcm; + } + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.c b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.c new file mode 100644 index 00000000..2c05b780 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.c @@ -0,0 +1,30 @@ +/* + * File: HelloWorld.cpp + * Author: navid + * + * Created on September 3, 2008, 2:17 PM + */ + + #include + #include + #include + #include "NativeFunctions.h" + + JNIEXPORT jlong JNICALL Java_test2_Main_getINodeNative + (JNIEnv *env, jobject obj, jstring filename) + { + struct stat status_buf; + jlong inodenum; + // stat("/home/navid/myfile.txt",&status_buf); + char *str = (*env)->GetStringUTFChars(env, filename, 0); + printf("\n"); + printf("File Name is: %s \n", str); + if (stat(str,&status_buf)<0) + inodenum = -1; + else + { + printf("Inode number is: %lu \n", status_buf.st_ino); + inodenum = status_buf.st_ino; + } + return inodenum; + } diff --git a/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.h b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.h new file mode 100644 index 00000000..4d9b0a28 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.h @@ -0,0 +1,19 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ + +#include + +#ifndef __test2_Main__ +#define __test2_Main__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +JNIEXPORT jlong JNICALL test2_Main_getINodeNative (JNIEnv *env, jobject, jstring); + +#ifdef __cplusplus +} +#endif + +#endif /* __HellWorld__ */ diff --git a/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.o b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.o new file mode 100644 index 00000000..b799c0af Binary files /dev/null and b/Robust/Transactions/TransactionalIOSrc/core/NativeFunctions.o differ diff --git a/Robust/Transactions/TransactionalIOSrc/core/TransactionLocalFileAttributes.java b/Robust/Transactions/TransactionalIOSrc/core/TransactionLocalFileAttributes.java new file mode 100644 index 00000000..3da69130 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/TransactionLocalFileAttributes.java @@ -0,0 +1,77 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.core; + + +import TransactionalIO.interfaces.OffsetDependency; +import java.io.RandomAccessFile; + +/** + * + * @author navid + */ +public class TransactionLocalFileAttributes { + + + private INode inode; + public boolean to_be_created = false; + RandomAccessFile f; + OffsetDependency offsetdependency; + private long copylocaloffset; + private boolean unknown_inital_offset_for_write = true; + private long localoffset; + private long localsize; + + public TransactionLocalFileAttributes(long initialoffset/*, long initialsize*/){ + localoffset = initialoffset; + copylocaloffset = initialoffset; + //copylocaloffset = 0; + unknown_inital_offset_for_write = true; + offsetdependency = OffsetDependency.NO_ACCESS; + //localsize = initialsize; + } + + public long getCopylocaloffset() { + return copylocaloffset; + } + + public void setCopylocaloffset(long copylocaloffset) { + this.copylocaloffset = copylocaloffset; + } + + public long getLocalsize() { + return localsize; + } + + public void setLocalsize(long localsize) { + this.localsize = localsize; + } + + + public OffsetDependency getOffsetdependency() { + return offsetdependency; + } + + public void setOffsetdependency(OffsetDependency offsetdependency) { + this.offsetdependency = offsetdependency; + } + + public boolean isUnknown_inital_offset_for_write() { + return unknown_inital_offset_for_write; + } + + public void setUnknown_inital_offset_for_write(boolean unknown_inital_offset_for_write) { + this.unknown_inital_offset_for_write = unknown_inital_offset_for_write; + } + + public long getLocaloffset() { + return localoffset; + } + + public void setLocaloffset(long localoffset) { + this.localoffset = localoffset; + } + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/TransactionalFile.java b/Robust/Transactions/TransactionalIOSrc/core/TransactionalFile.java new file mode 100644 index 00000000..72cdf445 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/TransactionalFile.java @@ -0,0 +1,1592 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.core; + + + +import TransactionalIO.Utilities.Range; +import TransactionalIO.exceptions.AbortedException; +import TransactionalIO.exceptions.PanicException; +import TransactionalIO.benchmarks.benchmark; +import TransactionalIO.core.ExtendedTransaction.Status; +import TransactionalIO.interfaces.BlockAccessModesEnum; +import TransactionalIO.interfaces.OffsetDependency; +import com.sun.org.apache.bcel.internal.generic.IFEQ; +import java.io.File; +import java.io.FileDescriptor; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.RandomAccessFile; +import java.nio.ByteBuffer; +import java.util.Collections; +import java.util.Iterator; +import java.util.SortedSet; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.Vector; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.logging.Level; +import java.util.logging.Logger; +import sun.misc.ConditionLock; + +/** + * + * @author navid + */ + + +public class TransactionalFile implements Comparable{ + + + private native int nativepread(byte buff[], long offset, int size, FileDescriptor fd); + + { + System.load("/home/navid/libkooni.so"); + } + + + public RandomAccessFile file; + private INode inode; + private int sequenceNum = 0; + public static int currenSeqNumforInode = 0; + /* public AtomicLong commitedoffset; + public AtomicLong commitedfilesize;*/ + public boolean to_be_created = false; + public boolean writemode = false; + public boolean appendmode = false; + public ReentrantLock offsetlock; + private GlobalOffset committedoffset; + private GlobalINodeState inodestate ; + + + public TransactionalFile(String filename, String mode) { + + + File f = new File(filename); + + if ((!(f.exists()))) { + to_be_created = true; + file = null; + + } else { + + try { + + offsetlock = new ReentrantLock(); + file = new RandomAccessFile(f, mode); + } catch (FileNotFoundException ex) { + + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + + } + inode = TransactionalFileWrapperFactory.getINodefromFileName(filename); + inodestate = TransactionalFileWrapperFactory.createTransactionalFile(inode, filename, mode); + + + sequenceNum = inodestate.seqNum; + inodestate.seqNum++; + + + + + + if (mode.equals("rw")) { + writemode = true; + } else if (mode.equals("a")) { + appendmode = true; + } + + if (inodestate != null) { + synchronized (inodestate) { + try { + // if (!(to_be_created)) { + // } else { + // adapter.commitedfilesize.set(0); + // } + + if (!appendmode) { + //commitedoffset.setOffsetnumber(0); + committedoffset = new GlobalOffset(0); + } else { + committedoffset = new GlobalOffset(file.length()); + } + + } catch (IOException ex) { + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + } + } + + + } + + private int invokeNativepread(byte buff[], long offset, int size) { + try { + return nativepread(buff, offset, size, file.getFD()); + } catch (IOException ex) { + + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + return -1; + } + + } + + + public int getSequenceNum() { + return sequenceNum; + } + + + public GlobalOffset getCommitedoffset() { + return committedoffset; + } + + public GlobalINodeState getInodestate() { + return inodestate; + } + + /* public TransactionalFile(Adapter adapter, RandomAccessFile file) { + + this.adapter = adapter; + this.file = file; + decriptors = new Vector(); + } + + public void copyTransactionalFile(TransactionalFile tf){ + try { + int tmp = tf.commitedoffset.get(); + boolean flag = tf.to_be_created; + FileDescriptor fd = tf.file.getFD(); + Adapter ad = new Adapter(tf.adapter); + } catch (IOException ex) { + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + }*/ + + /* public BlockDataStructure getBlockDataStructure(int blocknumber) { + synchronized (inodestate.lockmap) { + if (inodestate.lockmap.containsKey(blocknumber)) { + + return ((BlockDataStructure) (inodestate.lockmap.get(Long.valueOf(blocknumber)))); + } else { + + BlockDataStructure tmp = new BlockDataStructure(getInode(), blocknumber); + inodestate.lockmap.put(Integer.valueOf(blocknumber), tmp); + return tmp; + } + } + + }*/ + + + + public INode getInode() { + return inode; + } + /* public boolean deleteBlockLock(int blocknumber){ + synchronized(adapter.lockmap){ + //adapter.lockmap.get(blocknumber) + if (adapter.lockmap.containsKey(blocknumber)){ + if (((BlockLock)(adapter.lockmap.get(blocknumber))).referncount == 0){ + adapter.lockmap.remove(adapter.lockmap.get(blocknumber)); + return true; + } + else + return false; + } + else { + return false; + } + } + }*/ + public void close() { + try { + file.close(); + } catch (IOException ex) { + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public long getFilePointer(){ + + ExtendedTransaction me = Wrapper.getTransaction(); + TransactionLocalFileAttributes tmp = null; + + if (me == null) { + return non_Transactional_getFilePointer(); + } + + if (!(me.getGlobaltoLocalMappings().containsKey(this))){ + + //if (!(me.getFilesAccesses().containsKey(this.inode))) { + tmp = new TransactionLocalFileAttributes(0);/*, tf.getInodestate().commitedfilesize.get();*/ + + Vector dummy; + if (me.getAccessedFiles().containsKey(this.getInode())){ + dummy = (Vector) me.getAccessedFiles().get(this.getInode()); + } + else{ + dummy = new Vector(); + me.getAccessedFiles().put(this.getInode(), dummy); + } + + + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Unlocked the offset lock " + tf.offsetlock + " for file " + tf.getInode() + " form descriptor " + tf.getSequenceNum()); + dummy.add(this); + me.getGlobaltoLocalMappings().put(this, tmp); + me.merge_for_writes_done.put(this.getInode(), Boolean.TRUE); + + // me.addFile(this); + + //me.addFile(this); + } + + tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this); + if ((tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) || (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS)){ + tmp.setOffsetdependency(OffsetDependency.READ_DEPENDENCY); + //System.out.println("sad"); + //synchronized(this.committedoffset) + long target; + lockOffset(me); + //{ + + if (!(this.committedoffset.getOffsetReaders().contains(me))){ + this.committedoffset.getOffsetReaders().add(me); + /*synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + /* synchronized(benchmark.lock){ + System.out.println(Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + }*/ + } + + tmp.setLocaloffset(tmp.getLocaloffset() + this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset()); + target = this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset(); + + + offsetlock.unlock(); + //me.getHeldoffsetlocks().remove(offsetlock); + + Iterator it; + + if ((me.getWriteBuffer().get(inode)) != null) + { + + it = ((Vector) (me.getWriteBuffer().get(inode))).iterator(); + while (it.hasNext()){ + WriteOperations wrp = (WriteOperations) it.next(); + if (wrp.getBelongingto() == tmp && wrp.isUnknownoffset()) + wrp.setUnknownoffset(false); + /*wrp.getRange().setStart(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getStart()); + wrp.getRange().setEnd(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getEnd());*/ + wrp.getRange().setStart(target + wrp.getRange().getStart()); + wrp.getRange().setEnd(target + wrp.getRange().getEnd()); + } + } + + + //} + } + + + tmp.setUnknown_inital_offset_for_write(false); + + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Read the offset value for the file " + this.inode +" from descriptor " + this.sequenceNum + "\n"; + } + me.msg += Thread.currentThread().getName() + " Read the offset value for the file " + this.inode +" from descriptor " + this.sequenceNum + "\n";*/ + /* synchronized(benchmark.lock){ + System.out.println("offset " + Thread.currentThread() + " " + tmp.getLocaloffset()); + }*/ + return tmp.getLocaloffset(); + } + + public void seek(long offset) { + + if (appendmode) { + throw new PanicException("Cannot seek into a file opened in append mode"); + } + ExtendedTransaction me = Wrapper.getTransaction(); + + if (me == null) { + non_Transactional_Seek(offset); + return; + } + + else { + // if (me.getStatus() != Status.ACTIVE) + // throw new AbortedException(); + + TransactionLocalFileAttributes tmp = null; + //tf.offsetlock.lock(); + + //this.heldoffsetlocks.remove(tf.offsetlock); + //tf.offsetlock.unlock(); + if (!(me.getGlobaltoLocalMappings().containsKey(this))){ + //if (!(me.getFilesAccesses().containsKey(this.inode))) { + tmp = new TransactionLocalFileAttributes(offset);/*, tf.getInodestate().commitedfilesize.get();*/ + + Vector dummy; + if (me.getAccessedFiles().containsKey(this.getInode())){ + dummy = (Vector) me.getAccessedFiles().get(this.getInode()); + } + else{ + dummy = new Vector(); + me.getAccessedFiles().put(this.getInode(), dummy); + } + + + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Unlocked the offset lock " + tf.offsetlock + " for file " + tf.getInode() + " form descriptor " + tf.getSequenceNum()); + dummy.add(this); + me.getGlobaltoLocalMappings().put(this, tmp); + me.merge_for_writes_done.put(this.getInode(), Boolean.TRUE); + + //me.addFile(this); + } + tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this); + //tmp = ((TransactionLocalFileAttributes) (me.getFilesAccesses().get(this.getInode()))); + + if (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS) + tmp.setOffsetdependency(OffsetDependency.NO_DEPENDENCY); + + else if (tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) + tmp.setOffsetdependency(OffsetDependency.WRITE_DEPENDENCY_2); + + tmp.setUnknown_inital_offset_for_write(false); + + tmp.setLocaloffset(offset); + + + /* synchronized(benchmark.lock){ + System.out.println(tmp.getLocaloffset()); + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Seeked to the file" + this.inode +" from descriptor " + this.sequenceNum + "\n"); + } + } + + public int read(byte[] b) { + + if (appendmode) { + throw new PanicException("Cannot seek into a file opened in append mode"); + } + + boolean firsttime = false; + ExtendedTransaction me = Wrapper.getTransaction(); + int size = b.length; + int result = 0; + + + + if (me == null) { // not a transaction, but any I/O operation even though within a non-transaction is considered a single opertion transactiion + return non_Transactional_Read(b); + } + + //if (me.getStatus() != Status.ACTIVE) + // throw new AbortedException(); + + + if (me.getGlobaltoLocalMappings().containsKey(this)){ + + /*long target; + Vector locktracker = new Vector(); + TreeMap hm = me.getSortedFileAccessMap(me.getAccessedFiles());; + Vector vec = (Vector)hm.get(inode); + Iterator vecit = vec.iterator(); + while(vecit.hasNext()){ + TransactionalFile tr = (TransactionalFile)vecit.next(); + TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(tr); + + if ((tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) || (tmp.offsetdependency == OffsetDependency.NO_ACCESS) || (tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_2)){ + tmp.setUnknown_inital_offset_for_write(false); + tmp.setOffsetdependency(OffsetDependency.READ_DEPENDENCY); + tr.lockOffset(me); + System.out.printtln(Thread.currentThread() + " kiri"); + if (!(tr.committedoffset.getOffsetReaders().contains(me))){ + tr.committedoffset.getOffsetReaders().add(me); + target = this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset(); + me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + } + } + }*/ + + + + + TransactionLocalFileAttributes tmp = (TransactionLocalFileAttributes) me.getGlobaltoLocalMappings().get(this); + tmp.setUnknown_inital_offset_for_write(false); + if ((tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) || (tmp.offsetdependency == OffsetDependency.NO_ACCESS) || (tmp.getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_2)){ + //System.out.println(Thread.currentThread() + " here"); + //synchronized(this.committedoffset){ + lockOffset(me); + if (tmp.getOffsetdependency() != OffsetDependency.WRITE_DEPENDENCY_2){ + tmp.setLocaloffset(tmp.getLocaloffset() + this.committedoffset.getOffsetnumber() - tmp.getCopylocaloffset()); + } + + tmp.setOffsetdependency(OffsetDependency.READ_DEPENDENCY); + if (!(this.committedoffset.getOffsetReaders().contains(me))){ + this.committedoffset.getOffsetReaders().add(me); + /* synchronized(benchmark.lock){ + System.out.println("adding offset " + committedoffset + " " +Thread.currentThread()); + }*/ + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + /*synchronized(benchmark.lock){ + System.out.println(Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ this.sequenceNum); + }*/ + } + + offsetlock.unlock(); + // me.getHeldoffsetlocks().remove(offsetlock); + //} + } + Iterator it; + if (me.getWriteBuffer().get(inode) != null) + //if (!(((Vector)(me.getWriteBuffer().get(inode))).isEmpty())) + { + it = ((Vector) (me.getWriteBuffer().get(inode))).iterator(); + while (it.hasNext()){ + + WriteOperations wrp = (WriteOperations) it.next(); + if (wrp.isUnknownoffset()){ + wrp.setUnknownoffset(false); + //synchronized(wrp.getOwnertransactionalFile().committedoffset){ + wrp.getOwnertransactionalFile().lockOffset(me); + + wrp.getRange().setStart(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getStart()); + wrp.getRange().setEnd(wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset() + wrp.getRange().getEnd()); + if ((wrp.getBelongingto().getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_1) || (wrp.getBelongingto().offsetdependency == OffsetDependency.NO_ACCESS) || (wrp.getBelongingto().getOffsetdependency() == OffsetDependency.WRITE_DEPENDENCY_2)){ + wrp.getBelongingto().setOffsetdependency(OffsetDependency.READ_DEPENDENCY); + wrp.getBelongingto().setUnknown_inital_offset_for_write(false); + if (!(wrp.getOwnertransactionalFile().committedoffset.getOffsetReaders().contains(me))) + wrp.getOwnertransactionalFile().committedoffset.getOffsetReaders().add(me); + wrp.getBelongingto().setLocaloffset(wrp.getBelongingto().getLocaloffset() + wrp.getOwnertransactionalFile().committedoffset.getOffsetnumber() - wrp.getBelongingto().getCopylocaloffset()); + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ wrp.getOwnertransactionalFile().sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added to Offset Readers for file " + this.inode + " from descriptor "+ wrp.getOwnertransactionalFile().sequenceNum +"\n"); + } + + // me.getHeldoffsetlocks().remove(wrp.getOwnertransactionalFile().offsetlock); + wrp.getOwnertransactionalFile().offsetlock.unlock(); + + //} + + + + markAccessedBlocks(me, (int)wrp.getRange().getStart(), (int)(wrp.getRange().getEnd() - wrp.getRange().getStart()), BlockAccessModesEnum.WRITE); +// markWriteBlocks((int)wrp.getRange().getStart(), (int)(wrp.getRange().getEnd() - wrp.getRange().getStart())); + } + } + } + + /* if (!(me.isWritesmerged())){ + // synchronized(benchmark.lock){ + // System.out.println("ssssad " + Thread.currentThread() + " " + me.getWriteBuffer()); + // } + mergeWrittenData(); + }*/ + // System.out.println("ssssad " + Thread.currentThread() + " " + me.getWriteBuffer()); + if ((Boolean)me.merge_for_writes_done.get(inode) == Boolean.FALSE){ + // synchronized(benchmark.lock){ + System.out.println("ssssad " + Thread.currentThread() + " " + me.getWriteBuffer()); + mergeWrittenData(me); + //} + } + + + long loffset = tmp.getLocaloffset(); + markAccessedBlocks(me, loffset, size, BlockAccessModesEnum.READ); + + + Vector writebuffer; + if ((me.getWriteBuffer().get(this.inode)) != null) + writebuffer = (Vector) (me.getWriteBuffer().get(this.inode)); + else { + writebuffer = new Vector(); + me.getWriteBuffer().put(this.inode, writebuffer); + } + Range readrange = new Range(loffset, loffset + size); + Range writerange = null; + Range[] intersectedrange = new Range[writebuffer.size()]; + WriteOperations[] markedwriteop = new WriteOperations[writebuffer.size()]; + + int counter = 0; + + + + + boolean flag = false; + //System.out.println("yani che>??"); + + it = writebuffer.iterator(); + while (it.hasNext()) { + + WriteOperations wrp = (WriteOperations) it.next(); + writerange = wrp.getRange(); + if (writerange.includes(readrange)) { + markedwriteop[counter] = wrp; + flag = true; + break; + } + + if (writerange.hasIntersection(readrange)) { + intersectedrange[counter] = readrange.intersection(writerange); + markedwriteop[counter] = wrp; + + counter++; + } + } + + + // for block versioning mechanism + /*if (!(validateBlocksVersions(startblock, targetblock))) { ////check to see if version are still valid + + throw new AbortedException(); + + }*/ + if (flag) { + + result = readFromBuffer(b, tmp, markedwriteop[counter],writerange); + + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Read " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Read " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + return result; + } + + else{ + + if (counter == 0) { + /* synchronized(benchmark.lock){ + System.out.println("here" +Thread.currentThread()); + }*/ + + // lockOffset(me); + result = readFromFile(me, b, tmp); + } + else { + + for (int i = 0; i < counter; i++) { + + + Byte[] data = markedwriteop[i].getData(); + byte[] copydata = new byte[data.length]; + + for (int j = 0; j < data.length; j++) { + copydata[j] = data[j].byteValue(); + } + System.arraycopy(copydata, (int) (intersectedrange[i].getStart() - markedwriteop[i].getRange().getStart()), b, (int) (intersectedrange[i].getStart() - readrange.getStart()), (int) (Math.min(intersectedrange[i].getEnd(), readrange.getEnd()) - intersectedrange[i].getStart())); + result += Math.min(intersectedrange[i].getEnd(), readrange.getEnd()) - intersectedrange[i].getStart(); + } + + Range[] non_intersected_ranges = readrange.minus(intersectedrange, counter); + Vector occupiedblocks = new Vector(); + Vector heldlocks = new Vector(); + for (int i = 0; i < non_intersected_ranges.length; i++) { + int st = FileBlockManager.getCurrentFragmentIndexofTheFile(non_intersected_ranges[i].getStart()); + int en = FileBlockManager.getCurrentFragmentIndexofTheFile(non_intersected_ranges[i].getEnd()); + for (int j = st; j <= en; j++) { + if (!(occupiedblocks.contains(Integer.valueOf(j)))) { + occupiedblocks.add(Integer.valueOf(j)); + } + } + } + + + + lockOffset(me); + me.getHeldoffsetlocks().add(offsetlock); + + + for (int k = 0; k < occupiedblocks.size(); k++) { // locking the block locks + + while (me.getStatus() == Status.ACTIVE) { + + BlockDataStructure block = this.inodestate.getBlockDataStructure((Integer)(occupiedblocks.get(k)));//(BlockDataStructure) tmp.adapter.lockmap.get(Integer.valueOf(k))); + + //synchronized(block){ + + // if (block.getLock().readLock().tryLock()) { + block.getLock().readLock().lock(); + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Locked The Block Number " + block.getBlocknumber()+ " for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added The Block Number " + block.getBlocknumber()+ " for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + //synchronized (block){ + if (!(block.getReaders().contains(me))){ + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Added to Block Readers for Block Number " + block.getBlocknumber()+ " for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Added to Block Readers for Block Number " + block.getBlocknumber()+ " for file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + block.getReaders().add(me); + } + me.getHeldblocklocks().add(block.getLock().readLock()); + //heldlocks.add(block.getLock().readLock()); + //} + break; + // } + //} else { + // me.getContentionmanager().resolveConflict(me, block); + // } + //} + } + if (me.getStatus() == Status.ABORTED) { + //unlockLocks(heldlocks); + //offsetlock.unlock(); + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Aborted in locking blocks in read\n"); + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Aborted \n"; + }*/ + // Thread.currentThread().stop(); + throw new AbortedException(); + } + } + /* + int expvalue = ((Integer) tmp.getBlockversions().get(Integer.valueOf(k))).intValue(); + while (me.getStatus() == Status.ACTIVE) { + BlockDataStructure block = ((BlockDataStructure) tmp.adapter.lockmap.get(Integer.valueOf(k))); + if (block.getLock().tryLock()) { + heldlocks.add(block.getLock()); + if (!(block.getVersion().get() == expvalue)) { // for block versioning mechanism + me.abort(); + } + else { + break; + } + } + else { + me.getContentionManager().resolveConflict(me, block.getOwner()); + } + } + if (me.getStatus() == Status.ABORTED) { + unlockLocks(heldlocks); + offsetlock.unlock(); + throw new AbortedException(); + } + } + }*/ + + + for (int i = 0; i < non_intersected_ranges.length; i++) { + try { + synchronized(benchmark.lock){ + System.out.println("read start " + non_intersected_ranges[i].getStart()); + } + file.seek(non_intersected_ranges[i].getStart()); + int tmpsize = file.read(b, (int) (non_intersected_ranges[i].getStart() - readrange.getStart()), (int) (non_intersected_ranges[i].getEnd() - non_intersected_ranges[i].getStart())); + result += tmpsize; + } catch (IOException ex) { + + //unlockLocks(heldlocks); + //offsetlock.unlock(); + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } + } + me.unlockAllLocks(); + tmp.setLocaloffset(tmp.getLocaloffset() + result); + // unlockLocks(heldlocks); + // offsetlock.unlock(); + } + /*synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Read from file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Read from file " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + return result; + } + + } else { // add to the readers list + System.out.println("form read???"); + me.addFile(this); + return read(b); + } + + } + + public void write(byte[] data) throws IOException { + + if (!(writemode)) { + throw new IOException(); + + } + + ExtendedTransaction me = Wrapper.getTransaction(); + int size = data.length; + + + if (me == null) // not a transaction + { + + non_Transactional_Write(data); + return; + } + + //else if (me.getFilesAccesses().containsKey(this.getInode())) // + //{ + // if (me.getStatus() != Status.ACTIVE) + // throw new AbortedException(); + + if (me.getGlobaltoLocalMappings().containsKey(this)) // + { + + + Byte[] by = new Byte[size]; + for (int i = 0; i < size; i++) { + by[i] = Byte.valueOf(data[i]); + } + TransactionLocalFileAttributes tmp = ((TransactionLocalFileAttributes) (me.getGlobaltoLocalMappings().get(this))); + + /*if (appendmode) { + newwriterange = new Range((((Range) (tm.firstKey())).getStart()), (((Range) (tm.firstKey())).getEnd()) + size); + Range range = new Range((((Range) (tm.firstKey())).getStart()), (((Range) (tm.firstKey())).getEnd())); + Byte[] appenddata = new Byte[(int) (newwriterange.getEnd() - newwriterange.getStart())]; + Byte[] tempor = new Byte[(int) (range.getEnd() - range.getStart())]; + System.arraycopy(tempor, 0, appenddata, 0, tempor.length); + System.arraycopy(by, 0, appenddata, tempor.length, by.length); + tm.remove(range); + tm.put(newwriterange, appenddata); + tmp.setLocaloffset(loffset + size); + tmp.setFilelength(tmp.getFilelength() + size); + + return; + }*/ + Vector dummy; + if (((Vector)(me.getWriteBuffer().get(this.inode))) != null){ + dummy = new Vector((Vector)(me.getWriteBuffer().get(this.inode))); + } + else + dummy = new Vector(); + /* synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() + " gg " + tmp.getLocaloffset() + " " + (tmp.getLocaloffset()+by.length)); + }*/ + /*if (!(tmp.isUnknown_inital_offset_for_write())){ + lockOffset(me); + tmp.setLocaloffset(this.committedoffset.getOffsetnumber()); + offsetlock.unlock(); + }*/ + + dummy.add(new WriteOperations(by, new Range(tmp.getLocaloffset(), tmp.getLocaloffset() + by.length), tmp.isUnknown_inital_offset_for_write(), this, tmp)); + me.getWriteBuffer().put(this.inode, dummy); + + long loffset = tmp.getLocaloffset(); + + + tmp.setLocaloffset(tmp.getLocaloffset() + by.length); + + me.merge_for_writes_done.put(inode, Boolean.FALSE); + //me.setWritesmerged(false); + + + + if (!(tmp.isUnknown_inital_offset_for_write())){ + markAccessedBlocks(me, loffset, size, BlockAccessModesEnum.WRITE); +// markWriteBlocks(loffset, size); + } + /*{ + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(loffset); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size); + for (int i = startblock; i <= targetblock; i++) { + if (me.getAccessedBlocks().containsKey(Integer.valueOf(i))) { + if (((BlockAccessModesEnum) (me.getAccessedBlocks().get(Integer.valueOf(i)))) == BlockAccessModesEnum.READ) { + me.getAccessedBlocks().put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE); + } + } else { + me.getAccessedBlocks().put(Integer.valueOf(i), BlockAccessModesEnum.WRITE); + // tmp.getBlockversions().put(Integer.valueOf(i), Integer.valueOf(getBlockDataStructure(i).getVersion().get())); //For Block Versioning Mechanism + } + } + }*/ + + if (tmp.getOffsetdependency() == OffsetDependency.NO_ACCESS) + tmp.offsetdependency = OffsetDependency.WRITE_DEPENDENCY_1; + + + + /*if ((tmp.access_from_absolute_offset) || !(tmp.relocatablewrite)) + { + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(loffset); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size); + for (int i = startblock; i <= targetblock; i++) { + if (tmp.getAccesedblocks().containsKey(Integer.valueOf(i))) { + if (((BlockAccessModesEnum) (tmp.getAccesedblocks().get(Integer.valueOf(i)))) == BlockAccessModesEnum.READ) { + tmp.getAccesedblocks().put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE); + } + } else { + tmp.getAccesedblocks().put(Integer.valueOf(i), BlockAccessModesEnum.WRITE); + // tmp.getBlockversions().put(Integer.valueOf(i), Integer.valueOf(getBlockDataStructure(i).getVersion().get())); //For Block Versioning Mechanism + } + } + } + + if (tmp.access_from_absolute_offset){ + + mergeWrittenData(tmp.getNon_Speculative_Writtendata(), data, newwriterange); + tmp.setLocaloffset(loffset + size); + if (tmp.getLocaloffset() > tmp.getFilelength()) { + tmp.setFilelength(tmp.getLocaloffset()); + } + return; + } + + else // for comtimious determingin the accessed block is postpond till commit instant + { + Byte[] dd = new Byte[size]; + System.arraycopy(by, 0, dd, 0, size); + if (!(tmp.getSpeculative_Writtendata().isEmpty())){ + + Range lastrange = (Range) tmp.getSpeculative_Writtendata().lastKey(); + if (lastrange.getEnd() == newwriterange.getStart()){ + dd = new Byte[(int)(size + lastrange.getEnd() - lastrange.getStart())]; + System.arraycopy(((Byte[])tmp.getSpeculative_Writtendata().get(lastrange)), 0, dd, 0, (int)(lastrange.getEnd() - lastrange.getStart())); + System.arraycopy(by, 0, dd, (int)(lastrange.getEnd() - lastrange.getStart()), size); + newwriterange = new Range(lastrange.getStart(), size + lastrange.getEnd()); + tmp.getSpeculative_Writtendata().remove(lastrange); + } + + } + + tmp.getSpeculative_Writtendata().put(newwriterange, dd); + + tmp.setLocaloffset(loffset + size); + if (tmp.getLocaloffset() > tmp.getFilelength()) { + tmp.setFilelength(tmp.getLocaloffset()); + } + + return; + } + + */ + + /* + + if (tmp.accessmode == TransactionLocalFileAttributes.MODE.READ) + tmp.accessmode = TransactionLocalFileAttributes.MODE.READ_WRITE; + else if (tmp.accessmode == TransactionLocalFileAttributes.MODE.WRITE) + simpleWritetoBuffer(by, newwriterange, tm); + */ + // me.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Writes to " + this.inode + " from descriptor "+ this.sequenceNum +"\n"); + + } else { + // System.out.println("form write??"); + // me.addFile(this/*, TransactionLocalFileAttributes.MODE.WRITE*/); + //if (!(me.getGlobaltoLocalMappings().containsKey(this))){ + //if (!(me.getFilesAccesses().containsKey(this.inode))) { + TransactionLocalFileAttributes tmp = new TransactionLocalFileAttributes(0);/*, tf.getInodestate().commitedfilesize.get();*/ + + Vector dummy; + if (me.getAccessedFiles().containsKey(this.getInode())){ + dummy = (Vector) me.getAccessedFiles().get(this.getInode()); + } + else{ + dummy = new Vector(); + me.getAccessedFiles().put(this.getInode(), dummy); + } + + + + // this.msg.put(System.nanoTime(), Thread.currentThread().getName() + " Unlocked the offset lock " + tf.offsetlock + " for file " + tf.getInode() + " form descriptor " + tf.getSequenceNum()); + dummy.add(this); + me.getGlobaltoLocalMappings().put(this, tmp); + me.merge_for_writes_done.put(this.getInode(), Boolean.TRUE); + + //me.addFile(this); + //} + + write(data); + } + /* synchronized(benchmark.lock){ + benchmark.msg += Thread.currentThread().getName() + " Writes to " + this.inode + " from descriptor "+ this.sequenceNum +"\n"; + }*/ + + + } + + + private void markAccessedBlocks(ExtendedTransaction me,long loffset, int size, BlockAccessModesEnum mode){ + + TreeMap map; + + if (me.getAccessedBlocks().get(this.getInode()) != null) + map = (TreeMap) me.getAccessedBlocks().get(this.getInode()); + else{ + map = new TreeMap(); + me.getAccessedBlocks().put(this.inode, map); + } + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(loffset); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size); + for (int i = startblock; i <= targetblock; i++) { + if (map.containsKey(Integer.valueOf(i))){ + if (map.get(Integer.valueOf(i)) != mode){ + map.put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE); + } + } + else{ + map.put(Integer.valueOf(i), mode); + } + } + /*int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(loffset); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size); + for (int i = startblock; i <= targetblock; i++) { + if (me.getAccessedBlocks().containsKey(Integer.valueOf(i))) { + if (((BlockAccessModesEnum) (me.getAccessedBlocks().get(Integer.valueOf(i)))) == BlockAccessModesEnum.READ) { + me.getAccessedBlocks().put(Integer.valueOf(i), BlockAccessModesEnum.READ_WRITE); + } + + + } else { + me.getAccessedBlocks().put(Integer.valueOf(i), BlockAccessModesEnum.WRITE); + // tmp.getBlockversions().put(Integer.valueOf(i), Integer.valueOf(getBlockDataStructure(i).getVersion().get())); //For Block Versioning Mechanism + } + }*/ + } + +/* private void markWriteBlocks(long loffset, int size){ + ExtendedTransaction me = CustomThread.getTransaction(); + SortedSet tt; + if (me.writeBlocks.get(this.inode) != null) + tt = (SortedSet) me.writeBlocks.get(this.inode); + else { + tt = new TreeSet(); + me.writeBlocks.put(this.inode, tt); + } + + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(loffset); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(loffset, size); + for (int i = startblock; i <= targetblock; i++) { + tt.add(Integer.valueOf(i)); + } + }*/ + + private int readFromFile(ExtendedTransaction me, byte[] readdata, TransactionLocalFileAttributes tmp) { + + + //ExtendedTransaction me = Wrapper.getTransaction(); + int st = FileBlockManager.getCurrentFragmentIndexofTheFile(tmp.getLocaloffset()); + int end = FileBlockManager.getTargetFragmentIndexofTheFile(tmp.getLocaloffset(), readdata.length); + + BlockDataStructure block = null; + boolean locked = false; + for (int k = st; k <= end; k++) { + // int expvalue = ((Integer) tmp.getBlockversions().get(Integer.valueOf(k))).intValue(); // all comments here for versioning mechanism + while (me.getStatus() == Status.ACTIVE) { + //BlockDataStructure block = ((BlockDataStructure) this.inodestate.lockmap.get(Integer.valueOf(k))); + // BlockDataStructure block; + //if (me.getAccessedBlocks().get(inode)) + block = this.inodestate.getBlockDataStructure(Integer.valueOf(k)); + + block.getLock().readLock().lock(); + // me.getHeldblocklocks().add(block.getLock().readLock()); + if (!(block.getReaders().contains(me))){ + block.getReaders().add(me); + } + locked = true; + // if (!(block.getVersion().get() == expvalue)) { + // me.abort(); + // } else { + break; + // } + // } + /* else { + me.getContentionmanager().resolveConflict(me, block); + }*/ + + } + if (me.getStatus() == Status.ABORTED) { + int m; + if (locked){ + m = k+1; + } + else + m = k; + for (int i=st; i= wrp.getRange().getEnd()) { + + suffixdata = new Byte[(int) (wrp2.getRange().getEnd() - intersectedrange.getEnd())]; + suffixdata = (Byte[]) (wrp2.getData()); + startsuffix = intersectedrange.getEnd() - wrp2.getRange().getStart(); + suffixsize = (int)(wrp2.getRange().getEnd() - intersectedrange.getEnd()); + suffix = true; + //System.out.println("wrp2 > wrp"); + + endsuffix = wrp2.getRange().getEnd(); + //suffixstart = (int) (intersectedrange[i].getEnd() - intersectedrange[i].getStart()); + } + + else if (wrp.getRange().getEnd() > wrp2.getRange().getEnd()) { + suffixdata = new Byte[(int) (wrp.getRange().getEnd() - intersectedrange.getEnd())]; + suffixdata = (Byte[]) (wrp.getData()); + startsuffix = intersectedrange.getEnd() - wrp.getRange().getStart(); + suffixsize = (int)(wrp.getRange().getEnd() - intersectedrange.getEnd()); + // System.out.println("wrp2 < wrp"); + endsuffix = wrp.getRange().getEnd(); + suffix = true; + + } + /* System.out.println("prefix start:" + startprefix); + System.out.println("suffix end:" + endsuffix); + System.out.println("suffix start:" + startsuffix); + System.out.println("intermediate start:" + intermediatetart); + + System.out.println("suffix size:" + suffixsize);*/ + + + Byte[] data_to_insert; + + if ((prefix) && (suffix)) { + data_to_insert = new Byte[(int) (endsuffix - startprefix)]; + System.arraycopy(prefixdata, 0, data_to_insert, 0, prefixsize); + System.arraycopy(wrp2.getData(), (int)(intersectedrange.getStart() - wrp2.getRange().getStart()), data_to_insert, prefixsize, intermediatesize); + System.arraycopy(suffixdata, (int)startsuffix, data_to_insert, (prefixsize + intermediatesize), suffixsize); + wrp.setData(data_to_insert); + wrp.setRange(new Range(startprefix, endsuffix)); + } + + } + } + } + Iterator it = toberemoved.iterator(); + while (it.hasNext()) + vec.remove(it.next()); + + toberemoved.clear(); + Collections.sort(vec); + me.merge_for_writes_done.put(inode, Boolean.TRUE); + //me.setWritesmerged(true); + + /*} else if (prefix) { + data_to_insert = new Byte[(int) (to_be_merged_data_range.getStart() - start + size)]; + System.arraycopy(prefixdata, 0, data_to_insert, 0, (int) (to_be_merged_data_range.getStart() - start)); + System.arraycopy(by, 0, data_to_insert, (int) (to_be_merged_data_range.getStart() - start), size); + to_be_merged_data_range.setStart(start); + } else if (suffix) { + data_to_insert = new Byte[(int) (to_be_merged_data_range.getEnd() - end + size)]; + System.arraycopy(by, 0, data_to_insert, 0, size); + System.arraycopy(suffixdata, suffixstart, data_to_insert, size, (int) (end - to_be_merged_data_range.getEnd())); + to_be_merged_data_range.setEnd(end); + } else { + data_to_insert = new Byte[size]; + System.arraycopy(data_to_insert, (int) (to_be_merged_data_range.getStart() - start), by, 0, size); + }*/ + //target.put(to_be_merged_data_range, data_to_insert); + +/* + if (flag) { + int datasize = (int) (oldwriterange.getEnd() - oldwriterange.getStart()); + Byte[] original = (Byte[]) (wrp.getData()); + byte[] originaldata = new byte[datasize]; + + //for (int i = 0; i < data.length; i++) { + // originaldata[i] = original[i].byteValue(); + // } + System.arraycopy(data, 0, originaldata, (int) (to_be_merged_data_range.getStart() - oldwriterange.getStart()), size); + Byte[] to_be_inserted = new Byte[datasize]; + + for (int i = 0; i < datasize; i++) { + to_be_inserted[i] = Byte.valueOf(originaldata[i]); + } + target.put(oldwriterange, to_be_inserted); + return; + + } else if (counter == 0) { + target.put(to_be_merged_data_range, data); + return; + + } else { + + int suffixstart = 0; + long start = 0; + long end = 0; + Byte[] prefixdata = null; + Byte[] suffixdata = null; + boolean prefix = false; + boolean suffix = false; + + for (int i = 0; i < counter; i++) { + if (markedwriteranges[i].getStart() < to_be_merged_data_range.getStart()) { + prefixdata = new Byte[(int) (to_be_merged_data_range.getStart() - markedwriteranges[i].getStart())]; + prefixdata = (Byte[]) (target.get(markedwriteranges[i])); + start = markedwriteranges[i].getStart(); + //newdata = new Byte[size + newwriterange.getStart() - markedwriteranges[i].getStart()]; + //System.arraycopy(by, 0, newdata, newwriterange.getStart() - markedwriteranges[i].getStart(), size); + //System.arraycopy(originaldata, 0, newdata, 0, newwriterange.getStart() - markedwriteranges[i].getStart()); + + //newwriterange.setStart(markedwriteranges[i].getStart()); + prefix = true; + + + } else if (markedwriteranges[i].getEnd() > to_be_merged_data_range.getEnd()) { + + suffixdata = new Byte[(int) (markedwriteranges[i].getStart() - to_be_merged_data_range.getStart())]; + suffixdata = (Byte[]) (target.get(markedwriteranges[i])); + end = markedwriteranges[i].getEnd(); + + //Byte [] originaldata = (Byte [])(tmp.getWrittendata().get(markedwriteranges[i])); + //newdata = new Byte[size + newwriterange.getStart() - markedwriteranges[i].getStart()]; + //System.arraycopy(originaldata, 0, newdata, 0, newwriterange.getStart() - markedwriteranges[i].getStart()); + + //newwriterange.setStart(markedwriteranges[i].getStart()); + ///newwriterange.setEnd(markedwriteranges[i].getEnd()); + suffix = true; + suffixstart = (int) (intersectedrange[i].getEnd() - intersectedrange[i].getStart()); + //tm.remove(markedwriteranges[i]); + } + target.remove(markedwriteranges[i]); + + } + Byte[] data_to_insert; + + if ((prefix) && (suffix)) { + data_to_insert = new Byte[(int) (to_be_merged_data_range.getStart() - start + size - to_be_merged_data_range.getEnd() + end)]; + System.arraycopy(prefixdata, 0, data_to_insert, 0, (int) (to_be_merged_data_range.getStart() - start)); + System.arraycopy(by, 0, data_to_insert, (int) (to_be_merged_data_range.getStart() - start), size); + System.arraycopy(suffixdata, suffixstart, data_to_insert, (int) (size + to_be_merged_data_range.getStart() - start), (int) (end - to_be_merged_data_range.getEnd())); + to_be_merged_data_range.setStart(start); + to_be_merged_data_range.setEnd(end); + } else if (prefix) { + data_to_insert = new Byte[(int) (to_be_merged_data_range.getStart() - start + size)]; + System.arraycopy(prefixdata, 0, data_to_insert, 0, (int) (to_be_merged_data_range.getStart() - start)); + System.arraycopy(by, 0, data_to_insert, (int) (to_be_merged_data_range.getStart() - start), size); + to_be_merged_data_range.setStart(start); + } else if (suffix) { + data_to_insert = new Byte[(int) (to_be_merged_data_range.getEnd() - end + size)]; + System.arraycopy(by, 0, data_to_insert, 0, size); + System.arraycopy(suffixdata, suffixstart, data_to_insert, size, (int) (end - to_be_merged_data_range.getEnd())); + to_be_merged_data_range.setEnd(end); + } else { + data_to_insert = new Byte[size]; + System.arraycopy(data_to_insert, (int) (to_be_merged_data_range.getStart() - start), by, 0, size); + } + target.put(to_be_merged_data_range, data_to_insert); + return; + }*/ + + } + + public void non_Transactional_Write(byte[] data){ + + Vector heldlocks = new Vector(); + boolean flag = true; + offsetlock.lock(); + int startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(committedoffset.getOffsetnumber()); + int targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(committedoffset.getOffsetnumber(), data.length); + for (int i = startblock; i <= targetblock; i++) { + BlockDataStructure block =this.inodestate.getBlockDataStructure(i); + //if (block.getLock().writeLock().tryLock()) { + block.getLock().writeLock().lock(); + heldlocks.add(block.getLock().writeLock()); + //} else { + // unlockLocks(heldlocks); + // offsetlock.unlock(); + // flag = false; + // break; + //} + } + + /*if (flag) { + throw new PanicException("The I/O operation could not be done to contention for the file"); + }*/ + + //else { + try { + + file.seek(committedoffset.getOffsetnumber()); + file.write(data); + + committedoffset.setOffsetnumber(committedoffset.getOffsetnumber() +data.length); + + } catch (IOException ex) { + Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + } finally { + unlockLocks(heldlocks); + offsetlock.unlock(); + } + //} + } + + public int non_Transactional_Read(byte[] b){ + int size = -1; + Vector heldlocks = new Vector(); + boolean flag = true; + offsetlock.lock(); + int startblock; + int targetblock; + startblock = FileBlockManager.getCurrentFragmentIndexofTheFile(committedoffset.getOffsetnumber()); + targetblock = FileBlockManager.getTargetFragmentIndexofTheFile(committedoffset.getOffsetnumber(), size); + /* long offset = committedoffset.getOffsetnumber(); + committedoffset.setOffsetnumber(committedoffset.getOffsetnumber() +b.length); + if (!(committedoffset.getOffsetReaders().isEmpty())){ + Iterator it2 = committedoffset.getOffsetReaders().iterator(); // for visible readers strategy + while ( it2.hasNext()) + { + ExtendedTransaction tr = (ExtendedTransaction) it2.next(); + tr.abort(); + } + committedoffset.getOffsetReaders().clear(); + } + offsetlock.unlock();*/ + + for (int i = startblock; i <= targetblock; i++) { + BlockDataStructure block = this.inodestate.getBlockDataStructure(i); + //if (block.getLock().readLock().tryLock()) { + block.getLock().readLock().lock(); + heldlocks.add(block.getLock().readLock()); + /*} else { + unlockLocks(heldlocks); + offsetlock.unlock(); + flag = false; + break; + }*/ + } + /*if (flag) { + size = -1; + } else {*/ + + + + // try { + //ByteBuffer buffer = ByteBuffer.wrap(b); + //System.out.println(committedoffset.getOffsetnumber()); + //size = file.getChannel().read(buffer, offset); + //file.seek(committedoffset.getOffsetnumber()); + // size = file.read(b); + size = invokeNativepread(b, committedoffset.getOffsetnumber(), b.length); + + + committedoffset.setOffsetnumber(committedoffset.getOffsetnumber() +size); + if (!(committedoffset.getOffsetReaders().isEmpty())){ + Iterator it2 = committedoffset.getOffsetReaders().iterator(); // for visible readers strategy + while ( it2.hasNext()) + { + ExtendedTransaction tr = (ExtendedTransaction) it2.next(); + tr.abort(); + } + committedoffset.getOffsetReaders().clear(); + } + + // } catch (IOException ex) { + // Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + // size = -1; + // } finally { + unlockLocks(heldlocks); + offsetlock.unlock(); + if (size == 0) + size = -1; + // } + // } + return size; + + } + + public void non_Transactional_Seek(long offset){ + offsetlock.lock(); + //try { + //file.seek(offset); + //synchronized(adapter){ + committedoffset.setOffsetnumber(offset); + // inodestate.commitedfilesize.set(offset); + //} + //} catch (IOException ex) { + /// Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + //} finally { + offsetlock.unlock(); + //} + } + + public long non_Transactional_getFilePointer(){ + long offset = -1;; + offsetlock.lock(); + + + // try { + + //synchronized(adapter){ + offset = committedoffset.getOffsetnumber(); + //} + // } catch (IOException ex) { + // Logger.getLogger(TransactionalFile.class.getName()).log(Level.SEVERE, null, ex); + // } finally { + offsetlock.unlock(); + // } + return offset; + } + + public int compareTo(Object arg0) { + TransactionalFile tf = (TransactionalFile) arg0; + if (this.inode.getNumber() < tf.inode.getNumber()) + return -1; + else if (this.inode.getNumber() > tf.inode.getNumber()) + return 1; + else { + if (this.sequenceNum < tf.sequenceNum) + return -1; + else + return 1; + } + } + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/TransactionalFileWrapperFactory.java b/Robust/Transactions/TransactionalIOSrc/core/TransactionalFileWrapperFactory.java new file mode 100644 index 00000000..b684a556 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/TransactionalFileWrapperFactory.java @@ -0,0 +1,67 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package TransactionalIO.core; + +import java.io.File; +import java.util.HashMap; + +/** + * + * @author navid + */ +public class TransactionalFileWrapperFactory { + + + + private TransactionalFileWrapperFactory() { + } + // private static HashMap filemappings; + public static HashMap filemappings = new HashMap(); + + private static native long getINodeNative(String filename); + + static{ + //System.load("/home/navid/libnav.so"); + System.load("/home/navid/libnativeIO.so"); + } + + static INode getINodefromFileName(String filename) { + return new INode(getINodeNative(filename), filename); + } + + + + public synchronized static GlobalINodeState getTateransactionalFileINodeState(INode inode) { + + return (GlobalINodeState)filemappings.get(inode.getNumber()); + + } + public synchronized static GlobalINodeState createTransactionalFile(INode inode, String filename, String mode) { + + long inodenumber = inode.getNumber(); + if (inodenumber != -1) + + if (filemappings.containsKey(inode.getNumber())) { + // System.out.println("here"); + return (GlobalINodeState)filemappings.get(inode.getNumber()); + + } else { + + + long length = new File(filename).length(); + + GlobalINodeState inodestate = new GlobalINodeState(inode, length); + filemappings.put(inode.getNumber(), inodestate); + return inodestate; + } + + else + { + return null; + } + + } + +} diff --git a/Robust/Transactions/TransactionalIOSrc/core/Wrapper.java b/Robust/Transactions/TransactionalIOSrc/core/Wrapper.java new file mode 100644 index 00000000..b68f488e --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/Wrapper.java @@ -0,0 +1,115 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +import TransactionalIO.core.ExtendedTransaction; + +import TransactionalIO.core.CustomThread; +import TransactionalIO.interfaces.TransactionStatu; +import java.util.Vector; + + +/** + * + * @author navid + */ +public class Wrapper{ + + + private ExtendedTransaction transaction = new ExtendedTransaction(); + private static ThreadLocal IOtransactioncontainer = new ThreadLocal(); + private static ThreadLocal onMemoryAbort = new ThreadLocal(); + private static ThreadLocal onIOAbort = new ThreadLocal(); + + + + public static void prepareIOCommit(){ + + getTransaction().prepareCommit(); + } + + public static void commitIO(){ + getTransaction().commitChanges(); + } + + + + public static void Initialize(TransactionStatu memory){ + ExtendedTransaction transaction = new ExtendedTransaction(); + setTransaction(transaction); + transaction.setOtherSystem(memory); + + if (memory != null) + memory.setOtherSystem(transaction); + + + + /* setonIOAbort(new Vector()); + setonMemoryAbort(new Vector()); + + getonIOAbort().add(new terminateHandler() { + public void cleanup() { + Thread.getTransaction().abort(); + synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() +" KEWL"); + } + } + }); + + getonMemoryAbort().add(new terminateHandler() { + public void cleanup() { + CustomThread.getTransaction().abort(); + synchronized(benchmark.lock){ + System.out.println(Thread.currentThread() +" KEWL"); + } + } + });*/ + } + + public static void memoryCommit(){ + + } + + + public static void setTransaction(ExtendedTransaction transaction){ + IOtransactioncontainer.set(transaction); + } + + + + public static ExtendedTransaction getTransaction(){ + return (ExtendedTransaction) IOtransactioncontainer.get(); + } + + public static void setonIOAbort(Vector vec){ + onIOAbort.set(vec); + } + + + private static Vector getonIOAbort(){ + return (Vector) onIOAbort.get(); + } + + public static void setonMemoryAbort(Vector vec){ + onMemoryAbort.set(vec); + } + + + private static Vector getonMemoryAbort(){ + return (Vector) onMemoryAbort.get(); + } + + + + + + + + +} + + + diff --git a/Robust/Transactions/TransactionalIOSrc/core/WriteOperations.java b/Robust/Transactions/TransactionalIOSrc/core/WriteOperations.java new file mode 100644 index 00000000..9a376675 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/core/WriteOperations.java @@ -0,0 +1,82 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.core; + +import TransactionalIO.Utilities.Range; + +/** + * + * @author navid + */ +public class WriteOperations implements Comparable{ + + private Byte[] data; + private Range range; + private boolean unknownoffset; + private TransactionLocalFileAttributes belongingto; + private TransactionalFile ownertransactionalfile; + + public WriteOperations(Byte[] data, Range range, boolean unknownoffset, TransactionalFile ownertransactionalfile, TransactionLocalFileAttributes belongingto) { + this.data = data; + this.range = range; + this.unknownoffset = unknownoffset; + this.ownertransactionalfile = ownertransactionalfile; + this.belongingto = belongingto; + } + + public TransactionalFile getOwnertransactionalFile() { + return ownertransactionalfile; + } + + public void setOwnertransaction(TransactionalFile ownertransaction) { + this.ownertransactionalfile = ownertransaction; + } + + + + public Byte[] getData() { + return data; + } + + public Range getRange() { + return range; + } + + public boolean isUnknownoffset() { + return unknownoffset; + } + + public void setData(Byte[] data) { + this.data = new Byte[data.length]; + System.arraycopy(data, 0, this.data, 0, data.length); + } + + public void setRange(Range range) { + this.range = range; + } + + public void setUnknownoffset(boolean unknownoffset) { + this.unknownoffset = unknownoffset; + } + + public TransactionLocalFileAttributes getBelongingto() { + return belongingto; + } + + public void setBelongingto(TransactionLocalFileAttributes belongingto) { + this.belongingto = belongingto; + } + + + public int compareTo(Object other) { + WriteOperations tmp = (WriteOperations) other; + return this.range.compareTo(tmp.range); + } + + + + +} diff --git a/Robust/Transactions/TransactionalIOSrc/exceptions/PanicException.java b/Robust/Transactions/TransactionalIOSrc/exceptions/PanicException.java new file mode 100644 index 00000000..ed58fc60 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/exceptions/PanicException.java @@ -0,0 +1,69 @@ +/* + * PanicException.java + * + * Copyright 2006 Sun Microsystems, Inc., 4150 Network Circle, Santa + * Clara, California 95054, U.S.A. All rights reserved. + * + * Sun Microsystems, Inc. has intellectual property rights relating to + * technology embodied in the product that is described in this + * document. In particular, and without limitation, these + * intellectual property rights may include one or more of the + * U.S. patents listed at http://www.sun.com/patents and one or more + * additional patents or pending patent applications in the U.S. and + * in other countries. + * + * U.S. Government Rights - Commercial software. + * Government users are subject to the Sun Microsystems, Inc. standard + * license agreement and applicable provisions of the FAR and its + * supplements. Use is subject to license terms. Sun, Sun + * Microsystems, the Sun logo and Java are trademarks or registered + * trademarks of Sun Microsystems, Inc. in the U.S. and other + * countries. + * + * This product is covered and controlled by U.S. Export Control laws + * and may be subject to the export or import laws in other countries. + * Nuclear, missile, chemical biological weapons or nuclear maritime + * end uses or end users, whether direct or indirect, are strictly + * prohibited. Export or reexport to countries subject to + * U.S. embargo or to entities identified on U.S. export exclusion + * lists, including, but not limited to, the denied persons and + * specially designated nationals lists is strictly prohibited. + */ + +package TransactionalIO.exceptions; + +/** + * Thrown to indicate an error in the use of the transactional memory; + * that is, a violation of the assumptions of use. + */ +public class PanicException extends java.lang.RuntimeException { + + /** + * Creates new PanicException with no detail message. + */ + public PanicException() { + } + + public PanicException(String format, Object ... args) { + super(String.format(format, args)); + } + + /** + * Creates a new PanicException with the specified detail message. + * + * @param msg the detail message. + */ + public PanicException(String msg) { + super(msg); + } + + /** + * Creates an PanicException with the specified cause. + * + * @param cause Throwable that caused PanicException to be thrown + */ + public PanicException(Throwable cause) { + super(cause); + } +} + diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/BlockAccessModesEnum.java b/Robust/Transactions/TransactionalIOSrc/interfaces/BlockAccessModesEnum.java new file mode 100644 index 00000000..37ce6dd4 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/BlockAccessModesEnum.java @@ -0,0 +1,14 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +/** + * + * @author navid + */ +public enum BlockAccessModesEnum { + READ_WRITE, WRITE, READ +} diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/ContentionManager.java b/Robust/Transactions/TransactionalIOSrc/interfaces/ContentionManager.java new file mode 100644 index 00000000..af21ee96 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/ContentionManager.java @@ -0,0 +1,36 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +import TransactionalIO.core.BlockDataStructure; +import TransactionalIO.core.ExtendedTransaction; +import TransactionalIO.core.GlobalOffset; +import TransactionalIO.core.TransactionalFile; +import java.util.Vector; + +/** + * + * @author navid + */ + +public interface ContentionManager { + + + public void resolveConflict(ExtendedTransaction me, ExtendedTransaction other, GlobalOffset obj); + public void resolveConflict(ExtendedTransaction me, ExtendedTransaction other, BlockDataStructure obj); + + public void resolveConflict(ExtendedTransaction me, Vector/**/ other, GlobalOffset obj); + public void resolveConflict(ExtendedTransaction me, Vector/**/ other, BlockDataStructure obj); + + public void resolveConflict(ExtendedTransaction me, GlobalOffset obj); + public void resolveConflict(ExtendedTransaction me, BlockDataStructure obj); + + public long getPriority(); + + + public void setPriority(long value); + +} diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/FileAccessModesEum.java b/Robust/Transactions/TransactionalIOSrc/interfaces/FileAccessModesEum.java new file mode 100644 index 00000000..c956c781 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/FileAccessModesEum.java @@ -0,0 +1,17 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +import java.io.WriteAbortedException; + +/** + * + * @author navid + */ +public enum FileAccessModesEum { + READ_WRITE, APPEND, READ + +} diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/OffsetDependency.java b/Robust/Transactions/TransactionalIOSrc/interfaces/OffsetDependency.java new file mode 100644 index 00000000..a0f85f1c --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/OffsetDependency.java @@ -0,0 +1,15 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +/** + * + * @author navid + */ +public enum OffsetDependency { + NO_ACCESS, NO_DEPENDENCY, WRITE_DEPENDENCY_1, WRITE_DEPENDENCY_2, READ_DEPENDENCY + +} diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionStatu.java b/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionStatu.java new file mode 100644 index 00000000..4aeee172 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionStatu.java @@ -0,0 +1,23 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +import TransactionalIO.core.ExtendedTransaction.Status; + +/** + * + * @author navid + */ +public interface TransactionStatu { + + public void abortThisSystem(); + public TransactionStatu getOtherSystem(); + public void setOtherSystem(TransactionStatu othersystem); + public boolean isActive(); + public boolean isCommitted(); + public boolean isAborted(); + +} diff --git a/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionalProgram.java b/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionalProgram.java new file mode 100644 index 00000000..40a25165 --- /dev/null +++ b/Robust/Transactions/TransactionalIOSrc/interfaces/TransactionalProgram.java @@ -0,0 +1,15 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package TransactionalIO.interfaces; + +/** + * + * @author navid + */ +public interface TransactionalProgram { + public void execute(); + +}