Skip to content

Commit

Permalink
Fix init loggerSpace in concurrency (#193)
Browse files Browse the repository at this point in the history
Co-authored-by: “HzjNeverStop” <“[email protected]”>
  • Loading branch information
HzjNeverStop and “HzjNeverStop” authored Nov 7, 2023
1 parent 7841cae commit cbbb90e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.alipay.sofa.common</groupId>
<artifactId>sofa-common-tools</artifactId>
<version>2.0.2</version>
<version>2.0.3</version>
<packaging>jar</packaging>

<name>${project.groupId}:${project.artifactId}</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ private static AbstractLoggerSpaceFactory getILoggerFactoryBySpaceName(SpaceId s
if (!isSpaceILoggerFactoryExisted(spaceId)) {
factory = createILoggerFactory(spaceId, space, spaceClassloader);
space.setAbstractLoggerSpaceFactory(factory);
} else {
factory = LOG_FACTORY_MAP.get(spaceId).getAbstractLoggerSpaceFactory();
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 com.alipay.sofa.common.log;

import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author huzijie
* @version LoggerSpaceConcurrencyTest.java, v 0.1 2023年11月07日 3:57 PM huzijie Exp $
*/
public class LoggerSpaceConcurrencyTest {
private static final ThreadPoolExecutor EXECUTOR = new ThreadPoolExecutor(
5, 50, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(1000));


@Test
public void testLogUtilGetLoggerConcurrently() throws InterruptedException {
List<Logger> loggers = new CopyOnWriteArrayList<>();

CountDownLatch countDownLatch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
EXECUTOR.submit(() -> {
try {
Logger logger = getLogger();
loggers.add(logger);
} catch (Throwable t) {
t.printStackTrace();
} finally {
countDownLatch.countDown();
}
});
}

Assert.assertTrue(countDownLatch.await(10, TimeUnit.SECONDS));
Assert.assertEquals(loggers.size(), 10);

for (Logger logger : loggers) {
Assert.assertNotEquals("Constants.DEFAULT_LOG is not expected", logger, Constants.DEFAULT_LOG);
}

}

private Logger getLogger() {
return LoggerSpaceManager.getLoggerBySpace("TestLogger", "com.alipay.sofa.concurrency");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
<encoder charset="UTF-8">
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>

<logger name="com.foo.Bar" level="${logging.level.com.alipay.sofa.rpc}" additivity="false">
<appender-ref ref="stdout"/>
</logger>
<!-- <root level="DEBUG">
<appender-ref ref="stdout" />
</root>-->
</configuration>

0 comments on commit cbbb90e

Please sign in to comment.