Ejemplo n.º 1
0
  public void apply(
      ServerTransaction txn,
      Map objects,
      BackReferences includeIDs,
      ObjectInstanceMonitor instanceMonitor) {

    final ServerTransactionID stxnID = txn.getServerTransactionID();
    final ChannelID channelID = txn.getChannelID();
    final TransactionID txnID = txn.getTransactionID();
    final List changes = txn.getChanges();

    GlobalTransactionID gtxID = txn.getGlobalTransactionID();

    boolean active = isActive();

    for (Iterator i = changes.iterator(); i.hasNext(); ) {
      DNA orgDNA = (DNA) i.next();
      long version = orgDNA.getVersion();
      if (version == DNA.NULL_VERSION) {
        Assert.assertFalse(gtxID.isNull());
        version = gtxID.toLong();
      }
      DNA change = new VersionizedDNAWrapper(orgDNA, version, true);
      ManagedObject mo = (ManagedObject) objects.get(change.getObjectID());
      mo.apply(change, txnID, includeIDs, instanceMonitor, !active);
      if (active && !change.isDelta()) {
        // Only New objects reference are added here
        stateManager.addReference(txn.getChannelID(), mo.getID());
      }
    }

    Map newRoots = txn.getNewRoots();

    if (newRoots.size() > 0) {
      for (Iterator i = newRoots.entrySet().iterator(); i.hasNext(); ) {
        Entry entry = (Entry) i.next();
        String rootName = (String) entry.getKey();
        ObjectID newID = (ObjectID) entry.getValue();
        objectManager.createRoot(rootName, newID);
      }
    }
    if (active) {
      channelStats.notifyTransaction(channelID);
    }
    transactionRateCounter.increment();

    fireTransactionAppliedEvent(stxnID);
  }
Ejemplo n.º 2
0
  public void testWriteRead() throws IOException {
    long sequence = 0;
    ObjectStringSerializer serializer = new ObjectStringSerializer();
    TestCommitTransactionMessageFactory mf = new TestCommitTransactionMessageFactory();
    ChannelID channel = new ChannelID(69);
    TxnBatchID batchID = new TxnBatchID(42);

    List tx1Notifies = new LinkedList();
    // A nested transaction (all this buys us is more than 1 lock in a txn)
    LockID lid1 = new LockID("1");
    TransactionContext tc = new TransactionContext(lid1, TxnType.NORMAL, new LockID[] {lid1});
    ClientTransaction tmp =
        new ClientTransactionImpl(new TransactionID(101), new NullRuntimeLogger(), null);
    tmp.setTransactionContext(tc);
    LockID lid2 = new LockID("2");
    tc = new TransactionContext(lid2, TxnType.NORMAL, new LockID[] {lid1, lid2});
    ClientTransaction txn1 =
        new ClientTransactionImpl(new TransactionID(1), new NullRuntimeLogger(), null);
    txn1.setTransactionContext(tc);

    txn1.fieldChanged(
        new MockTCObject(new ObjectID(1), this), "class", "class.field", ObjectID.NULL_ID, -1);
    txn1.createObject(new MockTCObject(new ObjectID(2), this));
    txn1.createRoot("root", new ObjectID(3));
    for (int i = 0; i < 10; i++) {
      Notify notify = new Notify(new LockID("" + i), new ThreadID(i), i % 2 == 0);
      tx1Notifies.add(notify);
      txn1.addNotify(notify);
    }

    tc =
        new TransactionContext(new LockID("3"), TxnType.CONCURRENT, new LockID[] {new LockID("3")});
    ClientTransaction txn2 =
        new ClientTransactionImpl(new TransactionID(2), new NullRuntimeLogger(), null);
    txn2.setTransactionContext(tc);

    writer = new TransactionBatchWriter(batchID, serializer, encoding, mf);

    txn1.setSequenceID(new SequenceID(++sequence));
    txn2.setSequenceID(new SequenceID(++sequence));
    writer.addTransaction(txn1);
    writer.addTransaction(txn2);
    writer.wait4AllTxns2Serialize();

    TransactionBatchReaderImpl reader =
        new TransactionBatchReaderImpl(
            gidGenerator,
            writer.getData(),
            channel,
            new HashSet(),
            serializer,
            new ActiveServerTransactionFactory());
    assertEquals(2, reader.getNumTxns());
    assertEquals(batchID, reader.getBatchID());

    int count = 0;
    ServerTransaction txn;
    while ((txn = reader.getNextTransaction()) != null) {
      count++;
      assertEquals(channel, txn.getChannelID());
      assertEquals(count, txn.getTransactionID().toLong());

      switch (count) {
        case 1:
          assertEquals(2, txn.getChanges().size());
          assertEquals(1, txn.getNewRoots().size());
          assertEquals("root", txn.getNewRoots().keySet().toArray()[0]);
          assertEquals(new ObjectID(3), txn.getNewRoots().values().toArray()[0]);
          assertEquals(2, txn.getObjectIDs().size());
          assertTrue(
              txn.getObjectIDs()
                  .containsAll(Arrays.asList(new ObjectID[] {new ObjectID(1), new ObjectID(2)})));
          assertEquals(TxnType.NORMAL, txn.getTransactionType());
          assertTrue(
              Arrays.equals(new LockID[] {new LockID("1"), new LockID("2")}, txn.getLockIDs()));
          assertEquals(tx1Notifies, txn.getNotifies());
          break;
        case 2:
          assertEquals(0, txn.getChanges().size());
          assertEquals(0, txn.getNewRoots().size());
          assertEquals(0, txn.getObjectIDs().size());
          assertEquals(TxnType.CONCURRENT, txn.getTransactionType());
          assertTrue(Arrays.equals(new LockID[] {new LockID("3")}, txn.getLockIDs()));
          break;
        default:
          fail("count is " + count);
      }
    }

    assertEquals(2, count);
  }