Skip to content

Commit

Permalink
Merge branch 'master' into fixDuplicatePcJni
Browse files Browse the repository at this point in the history
  • Loading branch information
littleaj authored May 13, 2019
2 parents d794407 + 6fb3793 commit ba6a0d3
Show file tree
Hide file tree
Showing 27 changed files with 375 additions and 439 deletions.
2 changes: 1 addition & 1 deletion collectd/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ dependencies {
}

// Building the shadow (fat) jar after compiling sources.
assemble.dependsOn(shadowJar)
shadowJar.dependsOn assemble

30 changes: 9 additions & 21 deletions core/native.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ def javaIncludeDir = new File(System.env.'JAVA_HOME', "include")
def programFilesX86 = System.env.'ProgramFiles(x86)'
def referenceAssembliesDir = new File("$programFilesX86\\Reference Assemblies\\Microsoft\\Framework\\.NETFramework\\v4.5")

def winSdkDir = System.env.APPINSIGHTS_WIN_SDK_PATH ?: "C:\\Program Files (x86)\\Windows Kits\\8.1"
def vsToolsDir = System.env.APPINSIGHTS_VS_PATH ?: "C:\\Program Files (x86)\\Microsoft Visual Studio 14.0"

// region Native binary definition and configuration
ext {
isRelease = (System.properties["isRelease"] ?: "false").toBoolean()
}
model {
toolChains {
visualCpp(VisualCpp) {
windowsSdkDir winSdkDir
installDir vsToolsDir
}
}
buildTypes {
debug
release
}
platforms {
Expand All @@ -55,12 +60,6 @@ model {
targetPlatform "x86"
targetPlatform "x64"

if (isRelease) {
targetBuildTypes "release"
} else {
targetBuildTypes "debug"
}

baseName = "applicationinsights-core-native-win"

sources {
Expand Down Expand Up @@ -117,17 +116,6 @@ model {
cppCompiler.define "_UNICODE"
cppCompiler.define "_WINDLL"

if (System.env.WIN_SDK_LIB_PATH) {
logger.info "WIN_SDK_LIB_PATH=${System.env.WIN_SDK_LIB_PATH}"
if (targetPlatform == platforms.x86) {
linker.args "/LIBPATH:${System.env.WIN_SDK_LIB_PATH}\\um\\x86"
} else if (targetPlatform == platforms.x64) {
linker.args "/LIBPATH:${System.env.WIN_SDK_LIB_PATH}\\um\\x64"
}
} else {
logger.warn "WIN_SDK_LIB_PATH not set. Native build may fail. Use -DskipWinNative=true to skip"
}

if (buildType == buildTypes.release) {
cppCompiler.define 'NDEBUG'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

/**
* Created by gupele on 6/4/2015.
* @deprecated The TelemetryClient will always use the HTTP Client version shaded in core.
*/
@Deprecated
final class ApacheSender42 implements ApacheSender {

private final PoolingClientConnectionManager cm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,33 @@
final class ApacheSender43 implements ApacheSender {

private final AtomicReference<CloseableHttpClient> httpClientRef = new AtomicReference<>();
private volatile boolean isClientInitialized = false;
private final ExecutorService initializer = new ThreadPoolExecutor(0, 1, 2, TimeUnit.SECONDS, new LinkedBlockingDeque<Runnable>(),
ThreadPoolUtils.createNamedDaemonThreadFactory(ApacheSender43.class.getSimpleName()+"_initializer"));

public ApacheSender43() {
initializer.execute(new Runnable() {

@Override
public void run() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
cm.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
cm.setValidateAfterInactivity(REQUEST_TIMEOUT_IN_MILLIS);

httpClientRef.compareAndSet(null, HttpClients.custom()
.setConnectionManager(cm)
.useSystemProperties()
.build());
}
});
SDKShutdownActivity.INSTANCE.register(initializer);
}

static ApacheSender43 create() {
final ApacheSender43 sender = new ApacheSender43();
Thread initThread = new Thread(
new Runnable() {

@Override
public void run() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
cm.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);

sender.httpClientRef.compareAndSet(null, HttpClients.custom()
.setConnectionManager(cm)
.useSystemProperties()
.build());
synchronized (sender.httpClientRef) {
sender.httpClientRef.notifyAll();
}
}
}, ApacheSender43.class.getSimpleName()+"_initializer");
initThread.setDaemon(true);
initThread.start();
return sender;
}

private ApacheSender43() {}

@Override
public HttpResponse sendPostRequest(HttpPost post) throws IOException {
Expand Down Expand Up @@ -98,20 +103,15 @@ public void close() {

@Override
public HttpClient getHttpClient() {
if (!isClientInitialized) {
synchronized (this) {
if (!isClientInitialized) {
while (httpClientRef.get() == null) {
try {
TimeUnit.MILLISECONDS.sleep(3);
} catch (InterruptedException e){
}
}
isClientInitialized = true;
synchronized (httpClientRef) {
try {
while (httpClientRef.get() == null) {
httpClientRef.wait();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}

return httpClientRef.get();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,10 @@
public enum ApacheSenderFactory {
INSTANCE;

private ApacheSender apacheSender;
private ApacheSender apacheSender = ApacheSender43.create();

public synchronized ApacheSender create() {
if (apacheSender != null) {
return apacheSender;
}

if (!ClassDataUtils.INSTANCE.verifyClassExists("org.apache.http.conn.HttpClientConnectionManager")) {

InternalLogger.INSTANCE.warn("Found an old version of HttpClient jar, for best performance consider upgrading to version 4.3+");

apacheSender = new ApacheSender42();
} else {
InternalLogger.INSTANCE.trace("Using Http Client version 4.3+");
apacheSender = new ApacheSender43();
}
InternalLogger.INSTANCE.trace("Using Http Client version 4.3+");
return apacheSender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.shutdown.SDKShutdownActivity;
import com.microsoft.applicationinsights.internal.shutdown.Stoppable;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -39,7 +41,7 @@
* }}</pre>
* @since 2.4.0
*/
public final class PeriodicTaskManager {
public class PeriodicTaskPool implements Stoppable {

/**
* A Map which stores the currently active PeriodicTasks and it's associate future.
Expand All @@ -51,45 +53,22 @@ public final class PeriodicTaskManager {
*/
private final ScheduledExecutorService periodicTaskService;

/**
* Identifier for the executor service
*/
private final String TASK_POOL_NAME = "AI-SDK-PeriodicTaskManager-Pool";

/**
* Number of threads in the TheadPool associated with periodicTaskService.
*/
private final int poolSize;

/**
* The INSTANCE of {@link PeriodicTaskManager} class
*/
public static PeriodicTaskManager INSTANCE = null;

private PeriodicTaskManager(int poolSize) {
this.poolSize = poolSize;
this.periodicTaskService = new ScheduledThreadPoolExecutor(this.poolSize,
ThreadPoolUtils.createDaemonThreadFactory(PeriodicTaskManager.class, TASK_POOL_NAME));
this.periodicTaskMap = new ConcurrentHashMap<>();
SDKShutdownActivity.INSTANCE.register(this.periodicTaskService);
}

/**
* Initializer used to initialize the INSTANCE of {@link PeriodicTaskManager}
* @param poolSize
*/
public static void initializer(int poolSize) {

public PeriodicTaskPool(int poolSize, String poolName) {
if (poolSize < 1) {
throw new IllegalArgumentException("ThreadPool size should be at least 1.");
}
if (INSTANCE == null) {
synchronized (PeriodicTaskManager.class) {
if (INSTANCE == null) {
INSTANCE = new PeriodicTaskManager(poolSize);
}
}
if (StringUtils.isBlank(poolName)) {
throw new IllegalArgumentException("poolName must be non-empty");
}
this.poolSize = poolSize;
this.periodicTaskService = new ScheduledThreadPoolExecutor(this.poolSize,
ThreadPoolUtils.createNamedDaemonThreadFactory(poolName));
this.periodicTaskMap = new ConcurrentHashMap<>();
}

/**
Expand Down Expand Up @@ -141,6 +120,21 @@ public boolean cancelPeriodicTask(PeriodicRunnableTask task) {
return futureToCancel.cancel(true);
}

@Override
public void stop(long timeout, TimeUnit timeUnit) {
periodicTaskService.shutdown();
try {
if (!periodicTaskService.awaitTermination(timeout, timeUnit)) {
periodicTaskService.shutdownNow();
}
} catch (InterruptedException e) {
periodicTaskService.shutdownNow();
Thread.currentThread().interrupt();
} finally {
stopAndClear();
}
}


/**
* A Class that holds the instance of {@link Runnable} command along with it's unique taskId, initial delay,
Expand All @@ -154,16 +148,25 @@ public static final class PeriodicRunnableTask {
private final String taskId;

private PeriodicRunnableTask(Runnable command, long initialDelay, long period, TimeUnit unit, String taskId) {
validate(command, initialDelay, period, unit, taskId);
this.command = command;
this.initialDelay = initialDelay;
this.period = period;
this.unit = unit;
this.taskId = taskId;
}

public static PeriodicRunnableTask getInstance(Runnable command, long initialDelay, long period,
TimeUnit unit, String taskId) {
validate(command, initialDelay, period, unit, taskId);
/**
* Creates a PeriodicRunnableTask
* @param command The Runnable to execute
* @param initialDelay initial delay before running task for the first time.
* @param period after initial delay, period to execute task.
* @param unit timeUnit for initial delay and period
* @param taskId identifier for task
* @return
*/
public static PeriodicRunnableTask createTask(Runnable command, long initialDelay, long period,
TimeUnit unit, String taskId) {
return new PeriodicRunnableTask(command, initialDelay, period, unit, taskId);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.*;

public final class ApacheSender43Test {
@Test
public void testHttpClientType() {
HttpClient tested = new ApacheSender43().getHttpClient();
public void testHttpClientType() throws IOException {
HttpClient tested = ApacheSender43.create().getHttpClient();
assertNotNull(tested);

CloseableHttpClient httpClient = (CloseableHttpClient)tested;
assertNotNull(httpClient);
httpClient.close();
}
}
Loading

0 comments on commit ba6a0d3

Please sign in to comment.