<commit_before>/**
 *  Copyright 2014 Red Hat, Inc.
 *
 *  Red Hat 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.openshift.ping.dns;

import java.io.DataInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jgroups.Address;
import org.jgroups.annotations.MBean;
import org.jgroups.annotations.Property;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.protocols.FILE_PING;
import org.jgroups.protocols.PingData;
import org.openshift.ping.server.Server;
import org.openshift.ping.server.ServerFactory;
import org.openshift.ping.server.Utils;

@MBean(description = "DNS based discovery protocol")
public class DnsPing extends FILE_PING {

    public static final short OPENSHIFT_DNS_PING_ID = 2003;
    public static final short JGROUPS_DNS_PING_ID = 2004;
    static {
        ClassConfigurator.addProtocol(OPENSHIFT_DNS_PING_ID, DnsPing.class);
    }

    @Property
    private String serviceName;

    @Property
    private int servicePort;

    private ServerFactory factory;
    private Server server;

    public void setFactory(ServerFactory factory) {
        this.factory = factory;
    }

    @Override
    public void start() throws Exception {
        int svcPort = getServicePort(getServiceName());
        if (factory != null) {
            server = factory.getServer(svcPort);
        } else {
            server = Utils.getServer(svcPort);
        }
        final String serverName = server.getClass().getSimpleName();
        if (log.isInfoEnabled()) {
            log.info(String.format("Starting %s on port %s for channel address: %s", serverName, svcPort, stack.getChannel().getAddress()));
        }
        server.start(stack.getChannel());
        if (log.isInfoEnabled()) {
            log.info(String.format("%s started.", serverName));
        }
    }

    @Override
    public void stop() {
        try {
            final String serverName = server.getClass().getSimpleName();
            if (log.isInfoEnabled()) {
                log.info(String.format("Stopping server: %s", serverName));
            }
            server.stop(stack.getChannel());
            if (log.isInfoEnabled()) {
                log.info(String.format("%s stopped.", serverName));
            }
        } finally {
            super.stop();
        }
    }

    /**
     * Reads all information from the given directory under clusterName.
     *
     * @return all data
     */
    protected synchronized List<PingData> readAll(String clusterName) {
        List<PingData> retval = new ArrayList<>();
        String svcName = getServiceName();
        Set<String> svcHosts = getServiceHosts(svcName);
        int svcPort = getServicePort(svcName);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Reading service hosts %s on port [%s]", svcHosts, svcPort));
        }
        for (String svcHost : svcHosts) {
            try {
                PingData pingData = getPingData(svcHost, svcPort, clusterName);
                if (log.isDebugEnabled()) {
                    log.debug(String.format("Adding PingData [%s]", pingData));
                }
                retval.add(pingData);
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn(String.format("Problem getting PingData for cluster [%s], service [%s], host [%s], port [%s]; encountered [%s: %s]",
                            clusterName, svcName, svcHost, svcPort, e.getClass().getName(), e.getMessage()));
                }
            }
        }
        return retval;
    }

    private String getServiceName() {
        String svcName = trimToNull(this.serviceName);
        if (svcName == null) {
            svcName = trimToNull(System.getenv("OPENSHIFT_DNS_PING_SERVICE_NAME"));
            if (svcName == null) {
                svcName = "ping";
            }
        }
        return svcName;<commit_msg>Defer reporting until our entry is present<commit_after>/**
 *  Copyright 2014 Red Hat, Inc.
 *
 *  Red Hat 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.openshift.ping.dns;

import java.io.DataInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jgroups.Address;
import org.jgroups.annotations.MBean;
import org.jgroups.annotations.Property;
import org.jgroups.conf.ClassConfigurator;
import org.jgroups.protocols.FILE_PING;
import org.jgroups.protocols.PingData;
import org.openshift.ping.server.Server;
import org.openshift.ping.server.ServerFactory;
import org.openshift.ping.server.Utils;

@MBean(description = "DNS based discovery protocol")
public class DnsPing extends FILE_PING {

    public static final short OPENSHIFT_DNS_PING_ID = 2003;
    public static final short JGROUPS_DNS_PING_ID = 2004;
    static {
        ClassConfigurator.addProtocol(OPENSHIFT_DNS_PING_ID, DnsPing.class);
    }

    @Property
    private String serviceName;

    @Property
    private int servicePort;

    private ServerFactory factory;
    private Server server;

    public void setFactory(ServerFactory factory) {
        this.factory = factory;
    }

    @Override
    public void start() throws Exception {
        int svcPort = getServicePort(getServiceName());
        if (factory != null) {
            server = factory.getServer(svcPort);
        } else {
            server = Utils.getServer(svcPort);
        }
        final String serverName = server.getClass().getSimpleName();
        if (log.isInfoEnabled()) {
            log.info(String.format("Starting %s on port %s for channel address: %s", serverName, svcPort, stack.getChannel().getAddress()));
        }
        server.start(stack.getChannel());
        if (log.isInfoEnabled()) {
            log.info(String.format("%s started.", serverName));
        }
    }

    @Override
    public void stop() {
        try {
            final String serverName = server.getClass().getSimpleName();
            if (log.isInfoEnabled()) {
                log.info(String.format("Stopping server: %s", serverName));
            }
            server.stop(stack.getChannel());
            if (log.isInfoEnabled()) {
                log.info(String.format("%s stopped.", serverName));
            }
        } finally {
            super.stop();
        }
    }

    /**
     * Reads all information from the given directory under clusterName.
     *
     * @return all data
     */
    protected synchronized List<PingData> readAll(String clusterName) {
        List<PingData> retval = new ArrayList<>();
        String svcName = getServiceName();
        Set<String> svcHosts = getServiceHosts(svcName);
        int svcPort = getServicePort(svcName);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Reading service hosts %s on port [%s]", svcHosts, svcPort));
        }
        boolean localAddrPresent = false;
        for (String svcHost : svcHosts) {
            try {
                PingData pingData = getPingData(svcHost, svcPort, clusterName);
                localAddrPresent = localAddrPresent || pingData.getAddress().equals(local_addr);
                retval.add(pingData);
            } catch (Exception e) {
                if (log.isWarnEnabled()) {
                    log.warn(String.format("Problem getting PingData for cluster [%s], service [%s], host [%s], port [%s]; encountered [%s: %s]",
                            clusterName, svcName, svcHost, svcPort, e.getClass().getName(), e.getMessage()));
                }
            }
        }
        if (localAddrPresent) {
            if (log.isDebugEnabled()) {
                for(PingData pingData: retval) {
                    log.debug(String.format("Returning PingData [%s]", pingData));
                }
            }
            return retval;
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Local address not discovered, returning empty list");
            }
            return Collections.emptyList();
        }
    }

    private String getServiceName() {
        String svcName = trimToNull(this.serviceName);
        if (svcName == null) {
            svcName = trimToNull(System.getenv("OPENSHIFT_DNS_PING_SERVICE_NAME"));
            if (svcName == null) {
                svcName = "ping";
            }
        }
        return svcName;
{'fraction_non_alphanumeric': 0.10638075313807531, 'fraction_numerical': 0.0033472803347280333, 'mean_word_length': 2.3712976022566994, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5391146', 'n_tokens_mistral': 2731, 'n_tokens_neox': 2653, 'n_words': 702}