Search in sources :

Example 26 with DistributionManager

use of org.apache.geode.distributed.internal.DistributionManager in project geode by apache.

the class Connection method readAck.

/**
   * @param msToWait number of milliseconds to wait for an ack. If 0 then wait forever.
   * @param msInterval interval between checks
   * @throws SocketTimeoutException if msToWait expires.
   * @throws ConnectionException if ack is not received (fixes bug 34312)
   */
public void readAck(final int msToWait, final long msInterval, final DirectReplyProcessor processor) throws SocketTimeoutException, ConnectionException {
    if (isSocketClosed()) {
        throw new ConnectionException(LocalizedStrings.Connection_CONNECTION_IS_CLOSED.toLocalizedString());
    }
    synchronized (this.stateLock) {
        this.connectionState = STATE_READING_ACK;
    }
    boolean origSocketInUse = this.socketInUse;
    this.socketInUse = true;
    MsgReader msgReader = null;
    DMStats stats = owner.getConduit().stats;
    final Version version = getRemoteVersion();
    try {
        if (useNIO()) {
            msgReader = new NIOMsgReader(this, version);
        } else {
            msgReader = new OioMsgReader(this, version);
        }
        Header header = msgReader.readHeader();
        ReplyMessage msg;
        int len;
        if (header.getNioMessageType() == NORMAL_MSG_TYPE) {
            msg = (ReplyMessage) msgReader.readMessage(header);
            len = header.getNioMessageLength();
        } else {
            MsgDestreamer destreamer = obtainMsgDestreamer(header.getNioMessageId(), version);
            while (header.getNioMessageType() == CHUNKED_MSG_TYPE) {
                msgReader.readChunk(header, destreamer);
                header = msgReader.readHeader();
            }
            msgReader.readChunk(header, destreamer);
            msg = (ReplyMessage) destreamer.getMessage();
            releaseMsgDestreamer(header.getNioMessageId(), destreamer);
            len = destreamer.size();
        }
        // I'd really just like to call dispatchMessage here. However,
        // that call goes through a bunch of checks that knock about
        // 10% of the performance. Since this direct-ack stuff is all
        // about performance, we'll skip those checks. Skipping them
        // should be legit, because we just sent a message so we know
        // the member is already in our view, etc.
        DistributionManager dm = (DistributionManager) owner.getDM();
        msg.setBytesRead(len);
        msg.setSender(remoteAddr);
        stats.incReceivedMessages(1L);
        stats.incReceivedBytes(msg.getBytesRead());
        stats.incMessageChannelTime(msg.resetTimestamp());
        msg.process(dm, processor);
    // dispatchMessage(msg, len, false);
    } catch (MemberShunnedException e) {
    // do nothing
    } catch (SocketTimeoutException timeout) {
        throw timeout;
    } catch (IOException e) {
        final String err = LocalizedStrings.Connection_ACK_READ_IO_EXCEPTION_FOR_0.toLocalizedString(this);
        if (!isSocketClosed()) {
            if (logger.isDebugEnabled() && !isIgnorableIOException(e)) {
                logger.debug(err, e);
            }
        }
        try {
            requestClose(err + ": " + e);
        } catch (Exception ex) {
        }
        throw new ConnectionException(LocalizedStrings.Connection_UNABLE_TO_READ_DIRECT_ACK_BECAUSE_0.toLocalizedString(e));
    } catch (ConnectionException e) {
        this.owner.getConduit().getCancelCriterion().checkCancelInProgress(e);
        throw e;
    } catch (Exception e) {
        this.owner.getConduit().getCancelCriterion().checkCancelInProgress(e);
        if (!isSocketClosed()) {
            logger.fatal(LocalizedMessage.create(LocalizedStrings.Connection_ACK_READ_EXCEPTION), e);
        }
        try {
            requestClose(LocalizedStrings.Connection_ACK_READ_EXCEPTION_0.toLocalizedString(e));
        } catch (Exception ex) {
        }
        throw new ConnectionException(LocalizedStrings.Connection_UNABLE_TO_READ_DIRECT_ACK_BECAUSE_0.toLocalizedString(e));
    } finally {
        stats.incProcessedMessages(1L);
        accessed();
        this.socketInUse = origSocketInUse;
        if (this.ackTimedOut) {
            logger.info(LocalizedMessage.create(LocalizedStrings.Connection_FINISHED_WAITING_FOR_REPLY_FROM_0, new Object[] { getRemoteAddress() }));
            this.ackTimedOut = false;
        }
        if (msgReader != null) {
            msgReader.close();
        }
    }
    synchronized (stateLock) {
        this.connectionState = STATE_RECEIVED_ACK;
    }
}
Also used : DMStats(org.apache.geode.distributed.internal.DMStats) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) ReplyMessage(org.apache.geode.distributed.internal.ReplyMessage) DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) CancelException(org.apache.geode.CancelException) CancelledKeyException(java.nio.channels.CancelledKeyException) InterruptedIOException(java.io.InterruptedIOException) SocketException(java.net.SocketException) CacheClosedException(org.apache.geode.cache.CacheClosedException) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) ClosedChannelException(java.nio.channels.ClosedChannelException) IOException(java.io.IOException) ReplyException(org.apache.geode.distributed.internal.ReplyException) ClosedSelectorException(java.nio.channels.ClosedSelectorException) SocketTimeoutException(java.net.SocketTimeoutException) Header(org.apache.geode.internal.tcp.MsgReader.Header) Version(org.apache.geode.internal.Version) DistributionManager(org.apache.geode.distributed.internal.DistributionManager)

Example 27 with DistributionManager

use of org.apache.geode.distributed.internal.DistributionManager in project geode by apache.

the class AbstractExecution method executeFunctionOnLocalPRNode.

public void executeFunctionOnLocalPRNode(final Function fn, final FunctionContext cx, final PartitionedRegionFunctionResultSender sender, DM dm, boolean isTx) {
    if (dm instanceof DistributionManager && !isTx) {
        if (ServerConnection.isExecuteFunctionOnLocalNodeOnly().byteValue() == 1) {
            // executed locally
            ServerConnection.executeFunctionOnLocalNodeOnly((byte) 3);
            executeFunctionLocally(fn, cx, sender, dm);
            if (!sender.isLastResultReceived() && fn.hasResult()) {
                ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
            }
        } else {
            final DistributionManager newDM = (DistributionManager) dm;
            newDM.getFunctionExcecutor().execute(new Runnable() {

                public void run() {
                    executeFunctionLocally(fn, cx, sender, newDM);
                    if (!sender.isLastResultReceived() && fn.hasResult()) {
                        ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
                    }
                }
            });
        }
    } else {
        executeFunctionLocally(fn, cx, sender, dm);
        if (!sender.isLastResultReceived() && fn.hasResult()) {
            ((InternalResultSender) sender).setException(new FunctionException(LocalizedStrings.ExecuteFunction_THE_FUNCTION_0_DID_NOT_SENT_LAST_RESULT.toString(fn.getId())));
        }
    }
}
Also used : FunctionException(org.apache.geode.cache.execute.FunctionException) DistributionManager(org.apache.geode.distributed.internal.DistributionManager)

Example 28 with DistributionManager

use of org.apache.geode.distributed.internal.DistributionManager in project geode by apache.

the class RemoveBucketMessageTest method shouldBeMockable.

@Test
public void shouldBeMockable() throws Exception {
    RemoveBucketMessage mockRemoveBucketMessage = mock(RemoveBucketMessage.class);
    DistributionManager mockDistributionManager = mock(DistributionManager.class);
    PartitionedRegion mockPartitionedRegion = mock(PartitionedRegion.class);
    long startTime = System.currentTimeMillis();
    Object key = new Object();
    when(mockRemoveBucketMessage.operateOnPartitionedRegion(eq(mockDistributionManager), eq(mockPartitionedRegion), eq(startTime))).thenReturn(true);
    assertThat(mockRemoveBucketMessage.operateOnPartitionedRegion(mockDistributionManager, mockPartitionedRegion, startTime)).isTrue();
}
Also used : PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) UnitTest(org.apache.geode.test.junit.categories.UnitTest) Test(org.junit.Test)

Example 29 with DistributionManager

use of org.apache.geode.distributed.internal.DistributionManager in project geode by apache.

the class OutOfOffHeapMemoryDUnitTest method testSimpleOutOfOffHeapMemoryMemberDisconnects.

@Test
public void testSimpleOutOfOffHeapMemoryMemberDisconnects() {
    final DistributedSystem system = getSystem();
    final Cache cache = getCache();
    final DistributionManager dm = (DistributionManager) ((InternalDistributedSystem) system).getDistributionManager();
    Region<Object, Object> region = cache.createRegionFactory(getRegionShortcut()).setOffHeap(true).create(getRegionName());
    OutOfOffHeapMemoryException ooohme;
    try {
        Object value = new byte[1024];
        for (int i = 0; true; i++) {
            region.put("key-" + i, value);
        }
    } catch (OutOfOffHeapMemoryException e) {
        ooohme = e;
    }
    assertNotNull(ooohme);
    with().pollInterval(100, TimeUnit.MILLISECONDS).await().atMost(10, TimeUnit.SECONDS).until(() -> cache.isClosed() && !system.isConnected() && dm.isClosed());
    // wait for cache instance to be nulled out
    with().pollInterval(100, TimeUnit.MILLISECONDS).await().atMost(10, TimeUnit.SECONDS).until(() -> GemFireCacheImpl.getInstance() == null && InternalDistributedSystem.getAnyInstance() == null);
    assertNull(GemFireCacheImpl.getInstance());
    // verify system was closed out due to OutOfOffHeapMemoryException
    assertFalse(system.isConnected());
    InternalDistributedSystem ids = (InternalDistributedSystem) system;
    try {
        ids.getDistributionManager();
        fail("InternalDistributedSystem.getDistributionManager() should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
    // verify dm was closed out due to OutOfOffHeapMemoryException
    assertTrue(dm.isClosed());
    try {
        dm.throwIfDistributionStopped();
        fail("DistributionManager.throwIfDistributionStopped() should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
    // verify cache was closed out due to OutOfOffHeapMemoryException
    assertTrue(cache.isClosed());
    try {
        cache.getCancelCriterion().checkCancelInProgress(null);
        fail("GemFireCacheImpl.getCancelCriterion().checkCancelInProgress should throw DistributedSystemDisconnectedException");
    } catch (DistributedSystemDisconnectedException expected) {
        assertRootCause(expected, OutOfOffHeapMemoryException.class);
    }
}
Also used : DistributedSystemDisconnectedException(org.apache.geode.distributed.DistributedSystemDisconnectedException) OutOfOffHeapMemoryException(org.apache.geode.OutOfOffHeapMemoryException) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributedSystem(org.apache.geode.distributed.DistributedSystem) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Cache(org.apache.geode.cache.Cache) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 30 with DistributionManager

use of org.apache.geode.distributed.internal.DistributionManager in project geode by apache.

the class BackupDUnitTest method testBackupStatusCleanedUpAfterFailureOnOneMember.

@Test
public void testBackupStatusCleanedUpAfterFailureOnOneMember() throws Throwable {
    IgnoredException.addIgnoredException("Uncaught exception");
    IgnoredException.addIgnoredException("Stop processing");
    Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final VM vm2 = host.getVM(2);
    // Create an observer that will fail a backup
    // When this member receives a prepare
    DistributionMessageObserver observer = new SerializableDistributionMessageObserver() {

        @Override
        public void beforeProcessMessage(DistributionManager dm, DistributionMessage message) {
            if (message instanceof PrepareBackupRequest) {
                DistributionMessageObserver.setInstance(null);
                IOException exception = new IOException("Backup in progess");
                AdminFailureResponse response = AdminFailureResponse.create(dm, message.getSender(), exception);
                response.setMsgId(((PrepareBackupRequest) message).getMsgId());
                dm.putOutgoing(response);
                throw new RuntimeException("Stop processing");
            }
        }
    };
    vm0.invoke(() -> {
        disconnectFromDS();
        DistributionMessageObserver.setInstance(observer);
    });
    createPersistentRegion(vm0);
    createPersistentRegion(vm1);
    createData(vm0, 0, 5, "A", "region1");
    createData(vm0, 0, 5, "B", "region2");
    try {
        backup(vm2);
        fail("Backup should have failed with in progress exception");
    } catch (Exception expected) {
    // that's ok, hte backup should have failed
    }
    // A second backup should succeed because the observer
    // has been cleared and the backup state should be cleared.
    BackupStatus status = backup(vm2);
    assertEquals(2, status.getBackedUpDiskStores().size());
    assertEquals(Collections.emptySet(), status.getOfflineDiskStores());
}
Also used : AdminFailureResponse(org.apache.geode.internal.admin.remote.AdminFailureResponse) DistributionMessage(org.apache.geode.distributed.internal.DistributionMessage) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) DistributionMessageObserver(org.apache.geode.distributed.internal.DistributionMessageObserver) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) FileNotFoundException(java.io.FileNotFoundException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) IOException(java.io.IOException) PrepareBackupRequest(org.apache.geode.admin.internal.PrepareBackupRequest) BackupStatus(org.apache.geode.admin.BackupStatus) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

DistributionManager (org.apache.geode.distributed.internal.DistributionManager)60 Test (org.junit.Test)44 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)36 DistributionMessage (org.apache.geode.distributed.internal.DistributionMessage)29 DistributionMessageObserver (org.apache.geode.distributed.internal.DistributionMessageObserver)29 VM (org.apache.geode.test.dunit.VM)29 Host (org.apache.geode.test.dunit.Host)28 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)25 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)15 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)12 Cache (org.apache.geode.cache.Cache)11 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)11 AsyncInvocation (org.apache.geode.test.dunit.AsyncInvocation)11 IOException (java.io.IOException)9 IgnoredException (org.apache.geode.test.dunit.IgnoredException)9 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)9 Properties (java.util.Properties)8 UnitTest (org.apache.geode.test.junit.categories.UnitTest)8 RequestImageMessage (org.apache.geode.internal.cache.InitialImageOperation.RequestImageMessage)7 Region (org.apache.geode.cache.Region)6