<commit_before>package com.hp.octane.plugins.jenkins.bridge;

import com.hp.mqm.client.MqmRestClient;
import com.hp.octane.plugins.jenkins.actions.PluginActions;
import com.hp.octane.plugins.jenkins.client.JenkinsMqmRestClientFactory;
import com.hp.octane.plugins.jenkins.configuration.ServerConfiguration;
import net.sf.json.JSONArray;
import org.kohsuke.stapler.export.Exported;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * Created by gullery on 12/08/2015.
 * <p/>
 * This class encompasses functionality of managing connection/s to a single abridged client (MQM Server)
 */

public class BridgeClient {
	private static final Logger logger = Logger.getLogger(BridgeClient.class.getName());
	private static final String serverInstanceId = new PluginActions.ServerInfo().getInstanceId();
	private static int CONCURRENT_CONNECTIONS = 1;

	private ExecutorService connectivityExecutors = Executors.newFixedThreadPool(5, new AbridgedConnectivityExecutorsFactory());
	private ExecutorService taskProcessingExecutors = Executors.newFixedThreadPool(30, new AbridgedTasksExecutorsFactory());
	private AtomicInteger openedConnections = new AtomicInteger(0);

	private ServerConfiguration mqmConfiguration;
	private JenkinsMqmRestClientFactory restClientFactory;

	public BridgeClient(ServerConfiguration mqmConfiguration, JenkinsMqmRestClientFactory clientFactory) {
		this.mqmConfiguration = new ServerConfiguration(mqmConfiguration.location, mqmConfiguration.abridged, mqmConfiguration.sharedSpace, mqmConfiguration.username, mqmConfiguration.password, mqmConfiguration.impersonatedUser);
		this.restClientFactory = clientFactory;
		if (this.mqmConfiguration.abridged) connect();
		logger.info("BRIDGE: new bridge initialized for '" + this.mqmConfiguration.location + "', state: " + (this.mqmConfiguration.abridged ? "abridged" : "direct") + " connectivity");
	}

	public void update(ServerConfiguration mqmConfig) {
		this.mqmConfiguration = new ServerConfiguration(mqmConfig.location, mqmConfig.abridged, mqmConfig.sharedSpace, mqmConfig.username, mqmConfig.password, mqmConfig.impersonatedUser);
		if (mqmConfig.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) connect();
		logger.info("BRIDGE: updated for '" + this.mqmConfiguration.location + "', state: " + (this.mqmConfiguration.abridged ? "abridged" : "direct") + " connectivity");
	}

	private void connect() {
		connectivityExecutors.execute(new Runnable() {
			@Override
			public void run() {
				String tasksJSON;
				int totalConnections;
				try {
					totalConnections = openedConnections.incrementAndGet();
					logger.info("BRIDGE: connecting to '" + mqmConfiguration.location + "'...; total connections [including new one]: " + totalConnections);
					MqmRestClient restClient = restClientFactory.create(
							mqmConfiguration.location,
							mqmConfiguration.sharedSpace,
							mqmConfiguration.username,
							mqmConfiguration.password);
					tasksJSON = restClient.getAbridgedTasks(serverInstanceId);
					logger.info("BRIDGE: back from '" + mqmConfiguration.location + "' with " + (tasksJSON == null || tasksJSON.isEmpty() ? "no tasks" : "some tasks"));
					openedConnections.decrementAndGet();
					if (mqmConfiguration.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) {
						connect();
					}
					if (tasksJSON != null && !tasksJSON.isEmpty()) {
						dispatchTasks(tasksJSON);
					}
				} catch (Exception e) {
					openedConnections.decrementAndGet();
					logger.severe("BRIDGE: connection to MQM Server temporary failed: " + e.getMessage());
					try {
						Thread.sleep(500);
					} catch (InterruptedException ie) {
						logger.info("interrupted while breathing on temporary exception, continue to re-connect...");
					}
					if (mqmConfiguration.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) {
						connect();
					}
				}
			}
		});
	}

	private void dispatchTasks(String tasksJSON) {
		try {
			JSONArray tasks = JSONArray.fromObject(tasksJSON);
			logger.info("BRIDGE: going to process " + tasks.size() + " tasks");
			for (int i = 0; i < tasks.size(); i++) {
				taskProcessingExecutors.execute(new TaskProcessor(
						tasks.getJSONObject(i),
						restClientFactory,
						mqmConfiguration<commit_msg>tech: fixing the default abridged connectivity for UI config<commit_after>package com.hp.octane.plugins.jenkins.bridge;

import com.hp.mqm.client.MqmRestClient;
import com.hp.octane.plugins.jenkins.actions.PluginActions;
import com.hp.octane.plugins.jenkins.client.JenkinsMqmRestClientFactory;
import com.hp.octane.plugins.jenkins.configuration.ServerConfiguration;
import net.sf.json.JSONArray;
import org.kohsuke.stapler.export.Exported;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

/**
 * Created by gullery on 12/08/2015.
 * <p/>
 * This class encompasses functionality of managing connection/s to a single abridged client (MQM Server)
 */

public class BridgeClient {
	private static final Logger logger = Logger.getLogger(BridgeClient.class.getName());
	private static final String serverInstanceId = new PluginActions.ServerInfo().getInstanceId();
	private static int CONCURRENT_CONNECTIONS = 1;

	private ExecutorService connectivityExecutors = Executors.newFixedThreadPool(5, new AbridgedConnectivityExecutorsFactory());
	private ExecutorService taskProcessingExecutors = Executors.newFixedThreadPool(30, new AbridgedTasksExecutorsFactory());
	private AtomicInteger openedConnections = new AtomicInteger(0);

	private ServerConfiguration mqmConfiguration;
	private JenkinsMqmRestClientFactory restClientFactory;

	public BridgeClient(ServerConfiguration mqmConfiguration, JenkinsMqmRestClientFactory clientFactory) {
		this.mqmConfiguration = new ServerConfiguration(mqmConfiguration.location, mqmConfiguration.abridged, mqmConfiguration.sharedSpace, mqmConfiguration.username, mqmConfiguration.password, mqmConfiguration.impersonatedUser);
		this.restClientFactory = clientFactory;
		if (this.mqmConfiguration.abridged) connect();
		logger.info("BRIDGE: new bridge initialized for '" + this.mqmConfiguration.location + "', state: " + (this.mqmConfiguration.abridged ? "abridged" : "direct") + " connectivity");
	}

	public void update(ServerConfiguration mqmConfig) {
		this.mqmConfiguration = new ServerConfiguration(mqmConfig.location, mqmConfig.abridged, mqmConfig.sharedSpace, mqmConfig.username, mqmConfig.password, mqmConfig.impersonatedUser);
		if (mqmConfig.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) connect();
		logger.info("BRIDGE: updated for '" + this.mqmConfiguration.location + "', state: " + (this.mqmConfiguration.abridged ? "abridged" : "direct") + " connectivity");
	}

	private void connect() {
		connectivityExecutors.execute(new Runnable() {
			@Override
			public void run() {
				String tasksJSON;
				int totalConnections;
				try {
					totalConnections = openedConnections.incrementAndGet();
					logger.info("BRIDGE: connecting to '" + mqmConfiguration.location + "'...; total connections [including new one]: " + totalConnections);
					MqmRestClient restClient = restClientFactory.create(
							mqmConfiguration.location,
							mqmConfiguration.sharedSpace,
							mqmConfiguration.username,
							mqmConfiguration.password);
					tasksJSON = restClient.getAbridgedTasks(serverInstanceId);
					logger.info("BRIDGE: back from '" + mqmConfiguration.location + "' with " + (tasksJSON == null || tasksJSON.isEmpty() ? "no tasks" : "some tasks"));
					openedConnections.decrementAndGet();
					if (mqmConfiguration.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) {
						connect();
					}
					if (tasksJSON != null && !tasksJSON.isEmpty()) {
						dispatchTasks(tasksJSON);
					}
				} catch (Exception e) {
					openedConnections.decrementAndGet();
					logger.severe("BRIDGE: connection to MQM Server temporary failed: " + e.getMessage());
					try {
						Thread.sleep(1000);
					} catch (InterruptedException ie) {
						logger.info("interrupted while breathing on temporary exception, continue to re-connect...");
					}
					if (mqmConfiguration.abridged && openedConnections.get() < CONCURRENT_CONNECTIONS) {
						connect();
					}
				}
			}
		});
	}

	private void dispatchTasks(String tasksJSON) {
		try {
			JSONArray tasks = JSONArray.fromObject(tasksJSON);
			logger.info("BRIDGE: going to process " + tasks.size() + " tasks");
			for (int i = 0; i < tasks.size(); i++) {
				taskProcessingExecutors.execute(new TaskProcessor(
						tasks.getJSONObject(i),
						restClientFactory,
						mqmConfiguration
{'fraction_non_alphanumeric': 0.10716700473292766, 'fraction_numerical': 0.003944106378183457, 'mean_word_length': 4.984490896830748, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1377309', 'n_tokens_mistral': 2928, 'n_tokens_neox': 2507, 'n_words': 523}