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