Search in sources :

Example 26 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage in project apollo by ctripcorp.

the class ReleaseMessageServiceWithCacheTest method assembleReleaseMsg.

private ReleaseMessage assembleReleaseMsg(long id, String msgContent) {
    ReleaseMessage msg = new ReleaseMessage(msgContent);
    msg.setId(id);
    return msg;
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage)

Example 27 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage in project apollo by ctripcorp.

the class ReleaseMessageServiceWithCacheTest method testWhenHasReleaseMsgAndHasRepeatMsg.

@Test
public void testWhenHasReleaseMsgAndHasRepeatMsg() throws Exception {
    String someMsgContent = "msg1";
    ReleaseMessage someMsg = assembleReleaseMsg(1, someMsgContent);
    String anotherMsgContent = "msg2";
    ReleaseMessage anotherMsg = assembleReleaseMsg(2, anotherMsgContent);
    ReleaseMessage anotherRepeatMsg = assembleReleaseMsg(3, anotherMsgContent);
    when(releaseMessageRepository.findFirst500ByIdGreaterThanOrderByIdAsc(0L)).thenReturn(Arrays.asList(someMsg, anotherMsg, anotherRepeatMsg));
    releaseMessageServiceWithCache.afterPropertiesSet();
    verify(bizConfig).releaseMessageCacheScanInterval();
    ReleaseMessage latestReleaseMsg = releaseMessageServiceWithCache.findLatestReleaseMessageForMessages(Sets.newHashSet(someMsgContent, anotherMsgContent));
    assertNotNull(latestReleaseMsg);
    assertEquals(3, latestReleaseMsg.getId());
    assertEquals(anotherMsgContent, latestReleaseMsg.getMessage());
    List<ReleaseMessage> latestReleaseMsgGroupByMsgContent = releaseMessageServiceWithCache.findLatestReleaseMessagesGroupByMessages(Sets.newLinkedHashSet(Arrays.asList(someMsgContent, anotherMsgContent)));
    assertEquals(2, latestReleaseMsgGroupByMsgContent.size());
    assertEquals(3, latestReleaseMsgGroupByMsgContent.get(1).getId());
    assertEquals(anotherMsgContent, latestReleaseMsgGroupByMsgContent.get(1).getMessage());
    assertEquals(1, latestReleaseMsgGroupByMsgContent.get(0).getId());
    assertEquals(someMsgContent, latestReleaseMsgGroupByMsgContent.get(0).getMessage());
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) Test(org.junit.Test)

Example 28 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage in project apollo by ctripcorp.

the class ReleaseMessageScanner method recordMissingReleaseMessageIds.

private void recordMissingReleaseMessageIds(List<ReleaseMessage> messages, long startId) {
    for (ReleaseMessage message : messages) {
        long currentId = message.getId();
        if (currentId - startId > 1) {
            for (long i = startId + 1; i < currentId; i++) {
                missingReleaseMessages.putIfAbsent(i, 1);
            }
        }
        startId = currentId;
    }
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage)

Example 29 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage in project apollo by ctripcorp.

the class DatabaseMessageSender method cleanMessage.

private void cleanMessage(Long id) {
    // double check in case the release message is rolled back
    ReleaseMessage releaseMessage = releaseMessageRepository.findById(id).orElse(null);
    if (releaseMessage == null) {
        return;
    }
    boolean hasMore = true;
    while (hasMore && !Thread.currentThread().isInterrupted()) {
        List<ReleaseMessage> messages = releaseMessageRepository.findFirst100ByMessageAndIdLessThanOrderByIdAsc(releaseMessage.getMessage(), releaseMessage.getId());
        releaseMessageRepository.deleteAll(messages);
        hasMore = messages.size() == 100;
        messages.forEach(toRemove -> Tracer.logEvent(String.format("ReleaseMessage.Clean.%s", toRemove.getMessage()), String.valueOf(toRemove.getId())));
    }
}
Also used : ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage)

Example 30 with ReleaseMessage

use of com.ctrip.framework.apollo.biz.entity.ReleaseMessage in project apollo by ctripcorp.

the class DatabaseMessageSender method sendMessage.

@Override
@Transactional
public void sendMessage(String message, String channel) {
    logger.info("Sending message {} to channel {}", message, channel);
    if (!Objects.equals(channel, Topics.APOLLO_RELEASE_TOPIC)) {
        logger.warn("Channel {} not supported by DatabaseMessageSender!", channel);
        return;
    }
    Tracer.logEvent("Apollo.AdminService.ReleaseMessage", message);
    Transaction transaction = Tracer.newTransaction("Apollo.AdminService", "sendMessage");
    try {
        ReleaseMessage newMessage = releaseMessageRepository.save(new ReleaseMessage(message));
        toClean.offer(newMessage.getId());
        transaction.setStatus(Transaction.SUCCESS);
    } catch (Throwable ex) {
        logger.error("Sending message to database failed", ex);
        transaction.setStatus(ex);
        throw ex;
    } finally {
        transaction.complete();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ReleaseMessage(com.ctrip.framework.apollo.biz.entity.ReleaseMessage) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)30 Test (org.junit.Test)16 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)9 ResponseEntity (org.springframework.http.ResponseEntity)7 AbstractUnitTest (com.ctrip.framework.apollo.biz.AbstractUnitTest)3 Release (com.ctrip.framework.apollo.biz.entity.Release)3 ApolloNotificationMessages (com.ctrip.framework.apollo.core.dto.ApolloNotificationMessages)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 BizConfig (com.ctrip.framework.apollo.biz.config.BizConfig)2 ReleaseMessageRepository (com.ctrip.framework.apollo.biz.repository.ReleaseMessageRepository)2 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)2 Lists (com.google.common.collect.Lists)2 Sets (com.google.common.collect.Sets)2 SettableFuture (com.google.common.util.concurrent.SettableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 Awaitility (org.awaitility.Awaitility)2 Awaitility.await (org.awaitility.Awaitility.await)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertSame (org.junit.Assert.assertSame)2