All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.staros.journal.JournalReplayer Maven / Gradle / Ivy

There is a newer version: 3.4-rc2
Show newest version
// Copyright 2021-present StarRocks, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


package com.staros.journal;

import com.staros.exception.StarException;
import com.staros.journal.Journal;
import com.staros.manager.StarManager;
import com.staros.proto.CreateShardJournalInfo;
import com.staros.proto.DeleteShardGroupInfo;
import com.staros.proto.JournalEntry;
import com.staros.proto.JournalHeader;
import com.staros.proto.MetaGroupJournalInfo;
import com.staros.proto.OperationType;
import com.staros.service.Service;
import com.staros.service.ServiceTemplate;
import com.staros.shard.Shard;
import com.staros.shard.ShardGroup;
import com.staros.util.LogUtils;
import com.staros.worker.Worker;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.List;

public class JournalReplayer {
    private static final Logger LOG = LogManager.getLogger(JournalReplayer.class);

    private final StarManager starManager;

    public JournalReplayer(StarManager starManager) {
        this.starManager = starManager;
    }

    public void replay(Journal journal) {
        JournalEntry entry = journal.getEntry();
        JournalHeader header = entry.getHeader();

        String serviceId = header.getServiceId();
        OperationType operationType = header.getOperationType();

        LOG.debug("replay journal {} for service {}.", operationType, serviceId);

        try {
            switch (operationType) {
                case OP_REGISTER_SERVICE:
                    ServiceTemplate serviceTemplate = Journal.parseLogRegisterService(journal);
                    starManager.replayRegisterService(serviceTemplate);
                    break;
                case OP_DEREGISTER_SERVICE:
                    String serviceTemplateName = Journal.parseLogDeregisterService(journal);
                    starManager.replayDeregisterService(serviceTemplateName);
                    break;
                case OP_BOOTSTRAP_SERVICE:
                    Service service1 = Journal.parseLogBootstrapService(journal);
                    starManager.replayBootstrapService(service1);
                    break;
                case OP_SHUTDOWN_SERVICE:
                    Service service2 = Journal.parseLogShutdownService(journal);
                    starManager.replayShutdownService(service2);
                    break;
                case OP_CREATE_SHARD:
                    CreateShardJournalInfo createShardJournalInfo = Journal.parseLogCreateShard(journal);
                    starManager.replayCreateShard(serviceId, createShardJournalInfo);
                    break;
                case OP_DELETE_SHARD:
                    List shardIds = Journal.parseLogDeleteShard(journal);
                    starManager.replayDeleteShard(serviceId, shardIds);
                    break;
                case OP_UPDATE_SHARD:
                    List shardsToUpdate = Journal.parseLogUpdateShard(journal);
                    starManager.replayUpdateShard(serviceId, shardsToUpdate);
                    break;
                case OP_CREATE_SHARD_GROUP:
                    List shardGroups = Journal.parseLogCreateShardGroup(journal);
                    starManager.replayCreateShardGroup(serviceId, shardGroups);
                    break;
                case OP_DELETE_SHARD_GROUP:
                    DeleteShardGroupInfo info = Journal.parseLogDeleteShardGroup(journal);
                    starManager.replayDeleteShardGroup(serviceId, info);
                    break;
                case OP_CREATE_META_GROUP:
                    MetaGroupJournalInfo createMetaGroupJournalInfo = Journal.parseLogCreateMetaGroup(journal);
                    starManager.replayCreateMetaGroup(serviceId, createMetaGroupJournalInfo);
                    break;
                case OP_DELETE_META_GROUP:
                    MetaGroupJournalInfo deleteMetaGroupJournalInfo = Journal.parseLogDeleteMetaGroup(journal);
                    starManager.replayDeleteMetaGroup(serviceId, deleteMetaGroupJournalInfo);
                    break;
                case OP_UPDATE_META_GROUP:
                    MetaGroupJournalInfo updateMetaGroupJournalInfo = Journal.parseLogUpdateMetaGroup(journal);
                    starManager.replayUpdateMetaGroup(serviceId, updateMetaGroupJournalInfo);
                    break;
                case OP_ADD_WORKER:
                    Worker worker = Journal.parseLogAddWorker(journal);
                    starManager.replayAddWorker(serviceId, worker);
                    break;
                case OP_REMOVE_WORKER:
                    Pair pair = Journal.parseLogRemoveWorker(journal);
                    starManager.replayRemoveWorker(serviceId, pair.getKey(), pair.getValue());
                    break;
                case OP_UPDATE_WORKER:
                    List w = Journal.parseLogUpdateWorker(journal);
                    starManager.replayUpdateWorker(serviceId, w);
                    break;
                case OP_SET_ID:
                    long id = Journal.parseLogSetId(journal);
                    starManager.replaySetId(id);
                    break;
                default:
                    LOG.warn("unknown operation type {} when replay journal.", operationType);
                    break;
            }
        } catch (IOException e) {
            LogUtils.fatal(LOG, "replay journal {} for service {} failed, {}.", operationType, serviceId, e.getMessage());
        } catch (StarException e) {
            LogUtils.fatal(LOG, "replay journal {} for service {} failed, {}.", operationType, serviceId, e.getMessage());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy