<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2018 Lenovo
#
# 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
#
#     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.

import base64
import confluent.collective.invites as invites
import confluent.config.configmanager as cfm
import confluent.exceptions as exc
import confluent.log as log
import confluent.noderange as noderange
import confluent.tlvdata as tlvdata
import confluent.util as util
import eventlet
import eventlet.greenpool as greenpool
import eventlet.green.socket as socket
import eventlet.green.ssl as ssl
import eventlet.green.threading as threading
import confluent.sortutil as sortutil
import greenlet
import random
import time
import sys
try:
    import OpenSSL.crypto as crypto
except ImportError:
    # while not always required, we use pyopenssl required for at least
    # collective
    crypto = None

currentleader = None
follower = None
retrythread = None
failovercheck = None
initting = True
reassimilate = None

class ContextBool(object):
    def __init__(self):
        self.active = False
        self.mylock = threading.RLock()

    def __enter__(self):
        self.active = True
        self.mylock.__enter__()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.active = False
        self.mylock.__exit__(exc_type, exc_val, exc_tb)

connecting = ContextBool()
leader_init = ContextBool()

def connect_to_leader(cert=None, name=None, leader=None, remote=None):
    global currentleader
    global follower
    if leader is None:
        leader = currentleader
    log.log({'info': 'Attempting connection to leader {0}'.format(leader),
             'subsystem': 'collective'})
    try:
        remote = connect_to_collective(cert, leader, remote)
    except socket.error as e:
        log.log({'error': 'Collective connection attempt to {0} failed: {1}'
                          ''.format(leader, str(e)),
                 'subsystem': 'collective'})
        return False
    with connecting:
        with cfm._initlock:
            banner = tlvdata.recv(remote)  # the banner
            vers = banner.split()[2]
            if vers not in (b'v2', b'v3'):
                raise Exception('This instance only supports protocol 2 or 3, synchronize versions between collective members')
            tlvdata.recv(remote)  # authpassed... 0..
            if name is None:
                name = get_myname()
            tlvdata.send(remote, {'collective': {'operation': 'connect',
                                                 'name': name,
                                                 'txcount': cfm._txcount}})
            keydata = tlvdata.recv(remote)
            if not keydata:
                return False
            if 'error' in keydata:
                if 'backoff' in keydata:
                    log.log({
                        'info': 'Collective initialization in progress on '
                                '{0}'.format(leader),
                        'subsystem': 'collective'})
                    return False
                if 'leader' in keydata:
                    log.log(
                        {'info': 'Prospective leader {0} has redirected this '<commit_msg>Broaden scope of logging issues with connecting to a leader.<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4

# Copyright 2018 Lenovo
#
# 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
#
#     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.

import base64
import confluent.collective.invites as invites
import confluent.config.configmanager as cfm
import confluent.exceptions as exc
import confluent.log as log
import confluent.noderange as noderange
import confluent.tlvdata as tlvdata
import confluent.util as util
import eventlet
import eventlet.greenpool as greenpool
import eventlet.green.socket as socket
import eventlet.green.ssl as ssl
import eventlet.green.threading as threading
import confluent.sortutil as sortutil
import greenlet
import random
import time
import sys
try:
    import OpenSSL.crypto as crypto
except ImportError:
    # while not always required, we use pyopenssl required for at least
    # collective
    crypto = None

currentleader = None
follower = None
retrythread = None
failovercheck = None
initting = True
reassimilate = None

class ContextBool(object):
    def __init__(self):
        self.active = False
        self.mylock = threading.RLock()

    def __enter__(self):
        self.active = True
        self.mylock.__enter__()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.active = False
        self.mylock.__exit__(exc_type, exc_val, exc_tb)

connecting = ContextBool()
leader_init = ContextBool()

def connect_to_leader(cert=None, name=None, leader=None, remote=None):
    global currentleader
    global follower
    if leader is None:
        leader = currentleader
    log.log({'info': 'Attempting connection to leader {0}'.format(leader),
             'subsystem': 'collective'})
    try:
        remote = connect_to_collective(cert, leader, remote)
    except Exception as e:
        log.log({'error': 'Collective connection attempt to {0} failed: {1}'
                          ''.format(leader, str(e)),
                 'subsystem': 'collective'})
        return False
    with connecting:
        with cfm._initlock:
            banner = tlvdata.recv(remote)  # the banner
            vers = banner.split()[2]
            if vers not in (b'v2', b'v3'):
                raise Exception('This instance only supports protocol 2 or 3, synchronize versions between collective members')
            tlvdata.recv(remote)  # authpassed... 0..
            if name is None:
                name = get_myname()
            tlvdata.send(remote, {'collective': {'operation': 'connect',
                                                 'name': name,
                                                 'txcount': cfm._txcount}})
            keydata = tlvdata.recv(remote)
            if not keydata:
                return False
            if 'error' in keydata:
                if 'backoff' in keydata:
                    log.log({
                        'info': 'Collective initialization in progress on '
                                '{0}'.format(leader),
                        'subsystem': 'collective'})
                    return False
                if 'leader' in keydata:
                    log.log(
                        {'info': 'Prospective leader {0} has redirected this '
{'fraction_non_alphanumeric': 0.0813162904541746, 'fraction_numerical': 0.006527060103345118, 'mean_word_length': 2.3340888485947415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3010839', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1927, 'n_words': 685}