Cassandra 的啟動和初始化

Cassandra 的入口

CassandraDaemon 作為Cassandra的入口,做了以下幾件事:

  • load configuration
  • 注冊 MBean service
  • load schemas
  • scrub data directories
  • init keyspaces
  • start StorageService
  • start gossip service
  • start native/thrift server

main()

    public static void main(String[] args)
    {
        instance.activate();
    }

activate()

    public void activate()
    {
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        mbs.registerMBean(new StandardMBean(new NativeAccess(), NativeAccessMBean.class), new ObjectName(MBEAN_NAME));

        setup();
        start();
    }

setup()

    protected void setup()
    {
        // check the system keyspace to keep user from shooting self in foot by changing partitioner, cluster name, etc.
        // we do a one-off scrub of the system keyspace first; we can't load the list of the rest of the keyspaces,
        // until system keyspace is opened.
        for (CFMetaData cfm : Schema.instance.getKeyspaceMetaData(Keyspace.SYSTEM_KS).values())
            ColumnFamilyStore.scrubDataDirectories(cfm);
    
        SystemKeyspace.checkHealth();

        // load keyspace descriptions.
        DatabaseDescriptor.loadSchemas();

        // clean up compaction leftovers
        Map<Pair<String, String>, Map<Integer, UUID>> unfinishedCompactions = SystemKeyspace.getUnfinishedCompactions();

        // clean up debris in the rest of the keyspaces
        for (String keyspaceName : Schema.instance.getKeyspaces())
        {
            //...
        }

        Keyspace.setInitialized();

        // initialize keyspaces
        for (String keyspaceName : Schema.instance.getKeyspaces())
        {
            //...
        }

        // replay the log if necessary
        CommitLog.instance.recover();

        // start server internals
        StorageService.instance.registerDaemon(this);
        StorageService.instance.initServer();
        
        // Metrics
        String metricsReporterConfigFile = System.getProperty("cassandra.metricsReporterConfigFile");

        // schedule periodic background compaction task submission. this is simply a backstop against compactions stalling
        // due to scheduling errors or race conditions
        ScheduledExecutors.optionalTasks.scheduleWithFixedDelay(ColumnFamilyStore.getBackgroundCompactionTaskSubmitter(), 5, 1, TimeUnit.MINUTES);

        // Thrift
        thriftServer = new ThriftServer(rpcAddr, rpcPort, listenBacklog);
        // Native transport
        nativeServer = new org.apache.cassandra.transport.Server(nativeAddr, nativePort);

        completeSetup();
    }

start()

public void start()
{
    nativeServer.start();
    thriftServer.start();
}

初始化 DatabaseDescriptor

通過 static block 初始化

    static
    {
        applyConfig(loadConfig());
    }

首先是讀入config, 用yaml的library讀取config (org.yaml.snakeyaml).

    @VisibleForTesting
    public static Config loadConfig() throws ConfigurationException
    {
        String loaderClass = System.getProperty("cassandra.config.loader");
        ConfigurationLoader loader = loaderClass == null
                                   ? new YamlConfigurationLoader()
                                   : FBUtilities.<ConfigurationLoader>construct(loaderClass, "configuration loading");
        return loader.loadConfig();
    }

再apply config, 主要是validate config, 用config 初始化某些service比如snitch, comparator, memoryAllocator.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容