org.apache.druid.server.coordination.broker.DruidBroker.java Source code

Java tutorial

Introduction

Here is the source code for org.apache.druid.server.coordination.broker.DruidBroker.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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
 *
 *   http://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 org.apache.druid.server.coordination.broker;

import com.google.common.base.Predicates;
import com.google.common.util.concurrent.MoreExecutors;
import com.google.inject.Inject;
import org.apache.druid.client.FilteredServerInventoryView;
import org.apache.druid.client.ServerView;
import org.apache.druid.curator.discovery.ServiceAnnouncer;
import org.apache.druid.guice.ManageLifecycle;
import org.apache.druid.guice.annotations.Self;
import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
import org.apache.druid.java.util.common.lifecycle.LifecycleStop;
import org.apache.druid.server.DruidNode;

@ManageLifecycle
public class DruidBroker {
    private final DruidNode self;
    private final ServiceAnnouncer serviceAnnouncer;

    private volatile boolean started = false;

    @Inject
    public DruidBroker(final FilteredServerInventoryView serverInventoryView, final @Self DruidNode self,
            final ServiceAnnouncer serviceAnnouncer) {
        this.self = self;
        this.serviceAnnouncer = serviceAnnouncer;

        serverInventoryView.registerSegmentCallback(MoreExecutors.sameThreadExecutor(),
                new ServerView.BaseSegmentCallback() {
                    @Override
                    public ServerView.CallbackAction segmentViewInitialized() {
                        serviceAnnouncer.announce(self);
                        return ServerView.CallbackAction.UNREGISTER;
                    }
                },
                // We are not interested in any segment callbacks except view initialization
                Predicates.alwaysFalse());
    }

    @LifecycleStart
    public void start() {
        synchronized (self) {
            if (started) {
                return;
            }
            started = true;
        }
    }

    @LifecycleStop
    public void stop() {
        synchronized (self) {
            if (!started) {
                return;
            }
            serviceAnnouncer.unannounce(self);
            started = false;
        }
    }
}