Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.intent.persist; |
| 6 | |
| 7 | import com.esotericsoftware.kryo.Kryo; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 8 | import com.esotericsoftware.kryo.io.Output; |
| 9 | import edu.stanford.ramcloud.JRamCloud; |
| 10 | import java.io.ByteArrayOutputStream; |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 11 | import java.io.IOException; |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 12 | import java.nio.ByteBuffer; |
| 13 | import java.util.Arrays; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 14 | import java.util.concurrent.atomic.AtomicLong; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 15 | import net.onrc.onos.datagrid.web.IntentResource; |
| 16 | import net.onrc.onos.datastore.RCTable; |
| 17 | import net.onrc.onos.intent.IntentOperationList; |
| 18 | import net.onrc.onos.ofcontroller.networkgraph.INetworkGraphService; |
| 19 | import net.onrc.onos.ofcontroller.networkgraph.NetworkGraph; |
| 20 | import net.onrc.onos.registry.controller.IControllerRegistryService; |
| 21 | import net.onrc.onos.registry.controller.IdBlock; |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 22 | import org.slf4j.Logger; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 23 | import org.slf4j.LoggerFactory; |
| 24 | |
| 25 | /** |
| 26 | * |
| 27 | * @author nickkaranatsios |
| 28 | */ |
| 29 | public class PersistIntent { |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 30 | private final static Logger log = LoggerFactory.getLogger(IntentResource.class); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 31 | private long range = 10000L; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 32 | private final IControllerRegistryService controllerRegistry; |
| 33 | NetworkGraph graph = null; |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 34 | private final static String intentJournal = "G:IntentJournal"; |
| 35 | private final static int valueStoreLimit = 1024 * 1024; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 36 | private RCTable table; |
| 37 | private Kryo kryo = new Kryo(); |
| 38 | private ByteArrayOutputStream stream; |
| 39 | private Output output = null; |
| 40 | private AtomicLong nextId = null; |
| 41 | private long rangeEnd; |
| 42 | private IdBlock idBlock = null; |
| 43 | |
| 44 | |
| 45 | public PersistIntent(final IControllerRegistryService controllerRegistry, INetworkGraphService ng) { |
| 46 | this.controllerRegistry = controllerRegistry; |
| 47 | this.graph = ng.getNetworkGraph(); |
| 48 | table = RCTable.getTable(intentJournal); |
Nick Karanatsios | ed645df | 2014-02-20 23:22:29 -0800 | [diff] [blame] | 49 | stream = new ByteArrayOutputStream(1024); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 50 | output = new Output(stream); |
| 51 | } |
| 52 | |
| 53 | public long getKey() { |
| 54 | long key; |
| 55 | if (idBlock == null) { |
| 56 | key = getNextBlock(); |
| 57 | } else { |
| 58 | key = nextId.incrementAndGet(); |
| 59 | if (key >= rangeEnd) { |
| 60 | key = getNextBlock(); |
| 61 | } |
| 62 | } |
| 63 | return key; |
| 64 | } |
| 65 | |
| 66 | private long getNextBlock() { |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 67 | // XXX This method is not thread safe, may lose allocated IdBlock |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 68 | idBlock = controllerRegistry.allocateUniqueIdBlock(range); |
| 69 | nextId = new AtomicLong(idBlock.getStart()); |
| 70 | rangeEnd = idBlock.getEnd(); |
| 71 | return nextId.get(); |
| 72 | } |
| 73 | |
| 74 | public boolean persistIfLeader(long key, IntentOperationList operations) { |
| 75 | boolean leader = true; |
| 76 | boolean ret = false; |
| 77 | // TODO call controllerRegistry.isClusterLeader() |
| 78 | if (leader) { |
| 79 | try { |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 80 | // reserve key 10 entries for multi-write if size over 1MB |
| 81 | key *= 10; |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 82 | kryo.writeObject(output, operations); |
| 83 | output.close(); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 84 | ByteBuffer keyBytes = ByteBuffer.allocate(8).putLong(key); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 85 | byte[] buffer = stream.toByteArray(); |
Nick Karanatsios | a880057 | 2014-02-25 01:12:06 -0800 | [diff] [blame^] | 86 | int total = buffer.length; |
| 87 | if ((total >= valueStoreLimit )) { |
| 88 | int writeCount = total / valueStoreLimit; |
| 89 | int remainder = total % valueStoreLimit; |
| 90 | int upperIndex = 0; |
| 91 | for (int i = 0; i < writeCount; i++, key++) { |
| 92 | keyBytes.clear(); |
| 93 | keyBytes.putLong(key); |
| 94 | keyBytes.flip(); |
| 95 | upperIndex = (i * valueStoreLimit + valueStoreLimit) - 1; |
| 96 | log.debug("writing using indexes {}:{}", (i*valueStoreLimit) ,upperIndex); |
| 97 | table.create(keyBytes.array(), Arrays.copyOfRange(buffer, i * valueStoreLimit, upperIndex)); |
| 98 | } |
| 99 | if (remainder > 0) { |
| 100 | keyBytes.clear(); |
| 101 | keyBytes.putLong(key); |
| 102 | keyBytes.flip(); |
| 103 | log.debug("writing using indexes {}:{}" ,upperIndex ,total); |
| 104 | table.create(keyBytes.array(), Arrays.copyOfRange(buffer, upperIndex + 1, total - 1)); |
| 105 | } |
| 106 | } else { |
| 107 | keyBytes.flip(); |
| 108 | table.create(keyBytes.array(), buffer); |
| 109 | } |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 110 | log.debug("key is {} value length is {}", key, buffer.length); |
| 111 | stream.reset(); |
| 112 | stream.close(); |
Nick Karanatsios | a1bad35 | 2014-02-22 14:16:34 -0800 | [diff] [blame] | 113 | log.debug("persist operations to ramcloud size of operations: {}", operations.size()); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 114 | ret = true; |
| 115 | } catch (JRamCloud.ObjectExistsException ex) { |
| 116 | log.warn("Failed to store intent journal with key " + key); |
Nick Karanatsios | f933600 | 2014-02-24 11:26:58 -0800 | [diff] [blame] | 117 | } catch (IOException ex) { |
| 118 | log.error("Failed to close the stream"); |
Nick Karanatsios | 8abe717 | 2014-02-19 20:31:48 -0800 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | return ret; |
| 122 | } |
| 123 | } |