From 6040ee42d045f4dfaaf29a4cdd9ed93c6f20cff9 Mon Sep 17 00:00:00 2001 From: vempo Date: Wed, 4 Apr 2018 16:31:11 +0300 Subject: Minor improvements in hostname for logging Minor concurrency and naming improvements. Change-Id: I14dfb6d83dd5b84d5a8bb5cb67eda9f490b0e85a Issue-ID: SDC-772 Signed-off-by: vempo --- .../openecomp/sdc/logging/context/HostAddress.java | 89 ---------------------- .../sdc/logging/context/HostAddressCache.java | 89 ++++++++++++++++++++++ .../sdc/logging/slf4j/GlobalContextProvider.java | 4 +- .../sdc/logging/context/HostAddressCacheTest.java | 68 +++++++++++++++++ .../sdc/logging/context/HostAddressTest.java | 68 ----------------- 5 files changed, 159 insertions(+), 159 deletions(-) delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddress.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java create mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java delete mode 100644 openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java (limited to 'openecomp-be') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddress.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddress.java deleted file mode 100644 index bcfef6ca01..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddress.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * 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. - */ - -package org.openecomp.sdc.logging.context; - -import java.net.InetAddress; -import java.net.UnknownHostException; - -/** - * Holds a reference to local host address as returned by Java runtime. A value of host address will be cached for the - * interval specified in the constructor or {@link #DEFAULT_REFRESH_INTERVAL}. The caching helps to avoid many low-level - * calls, but at the same time pick up any IP or FQDN changes. Although the underlying JDK implementation uses caching - * too, the refresh interval for logging may be much longer due to the nature of the use. - * - * @author evitaliy - * @since 26 Mar 2018 - */ -@SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace", "squid:S106", "squid:S1148"}) -public class HostAddress { - - private static final long DEFAULT_REFRESH_INTERVAL = 60000L; // 1 min - - private final long interval; - - private CacheEntry cachedAddress; - - public HostAddress() { - this(DEFAULT_REFRESH_INTERVAL); - } - - /** - * Creates a cache for host address with a custom refresh interval. - */ - public HostAddress(long refreshInterval) { - this.interval = refreshInterval; - this.cachedAddress = new CacheEntry(System.currentTimeMillis(), read()); - } - - /** - * Returns an address (host name and IP address) of the local system. - * - * @return local host address or null if it could not be read for some reason - */ - public synchronized InetAddress get() { - - long current = System.currentTimeMillis(); - if (current - cachedAddress.lastUpdated < interval) { - return cachedAddress.address; - } - - InetAddress address = read(); - cachedAddress = new CacheEntry(current, address); - return address; - } - - private InetAddress read() { - - try { - return InetAddress.getLocalHost(); - } catch (UnknownHostException e) { - e.printStackTrace(); // can't really use logging - return null; // let register the attempt even if failed - } - } - - private static class CacheEntry { - - private final long lastUpdated; - private final InetAddress address; - - private CacheEntry(long lastUpdated, InetAddress address) { - this.lastUpdated = lastUpdated; - this.address = address; - } - } -} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java new file mode 100644 index 0000000000..afe2b0b1d1 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/context/HostAddressCache.java @@ -0,0 +1,89 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.logging.context; + +import java.net.InetAddress; +import java.net.UnknownHostException; + +/** + * Holds a reference to local host address as returned by Java runtime. A value of host address will be cached for the + * interval specified in the constructor or {@link #DEFAULT_REFRESH_INTERVAL}. The caching helps to avoid many low-level + * calls, but at the same time pick up any IP or FQDN changes. Although the underlying JDK implementation uses caching + * too, the refresh interval for logging may be much longer due to the nature of the use. + * + * @author evitaliy + * @since 26 Mar 2018 + */ +@SuppressWarnings({"UseOfSystemOutOrSystemErr", "CallToPrintStackTrace", "squid:S106", "squid:S1148"}) +public class HostAddressCache { + + private static final long DEFAULT_REFRESH_INTERVAL = 60000L; // 1 min + + private final long interval; + + private volatile CacheEntry cachedAddress; + + public HostAddressCache() { + this(DEFAULT_REFRESH_INTERVAL); + } + + /** + * Creates a cache for host address with a custom refresh interval. + */ + public HostAddressCache(long refreshInterval) { + this.interval = refreshInterval; + this.cachedAddress = new CacheEntry(System.currentTimeMillis(), read()); + } + + /** + * Returns an address (host name and IP address) of the local system. + * + * @return local host address or null if it could not be read for some reason + */ + public synchronized InetAddress get() { + + long current = System.currentTimeMillis(); + if (current - cachedAddress.lastUpdated < interval) { + return cachedAddress.address; + } + + InetAddress address = read(); + cachedAddress = new CacheEntry(current, address); + return address; + } + + private InetAddress read() { + + try { + return InetAddress.getLocalHost(); + } catch (UnknownHostException e) { + e.printStackTrace(); // can't really use logging + return null; // let register the attempt even if failed + } + } + + private static class CacheEntry { + + private final long lastUpdated; + private final InetAddress address; + + private CacheEntry(long lastUpdated, InetAddress address) { + this.lastUpdated = lastUpdated; + this.address = address; + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java index 5f2963ec40..97cac2e9ca 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/main/java/org/openecomp/sdc/logging/slf4j/GlobalContextProvider.java @@ -19,7 +19,7 @@ package org.openecomp.sdc.logging.slf4j; import java.net.InetAddress; import java.util.EnumMap; import java.util.Map; -import org.openecomp.sdc.logging.context.HostAddress; +import org.openecomp.sdc.logging.context.HostAddressCache; import org.openecomp.sdc.logging.context.InstanceId; /** @@ -30,7 +30,7 @@ import org.openecomp.sdc.logging.context.InstanceId; */ class GlobalContextProvider implements ContextProvider { - private static final HostAddress HOST_ADDRESS_CACHE = new HostAddress(); + private static final HostAddressCache HOST_ADDRESS_CACHE = new HostAddressCache(); @Override public Map values() { diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java new file mode 100644 index 0000000000..6d7a636840 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressCacheTest.java @@ -0,0 +1,68 @@ +/* + * Copyright © 2016-2018 European Support Limited + * + * 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. + */ + +package org.openecomp.sdc.logging.context; + +import static org.testng.Assert.assertNotNull; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import org.easymock.EasyMock; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.testng.PowerMockTestCase; +import org.testng.annotations.Test; + +/** + * Retrieval and caching of host address. + * + * @author evitaliy + * @since 28 Mar 2018 + */ +@PrepareForTest(InetAddress.class) +public class HostAddressCacheTest extends PowerMockTestCase { + + @Test + public void hostAddressIsAlwaysPopulated() { + assertNotNull(new HostAddressCache().get()); + } + + @Test + public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException { + mockInetAddress(1); + HostAddressCache addressCache = new HostAddressCache(1000); + addressCache.get(); + addressCache.get(); + } + + @Test + public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException { + mockInetAddress(2); + HostAddressCache addressCache = new HostAddressCache(-1); + addressCache.get(); + addressCache.get(); + } + + private void mockInetAddress(int times) throws UnknownHostException { + InetAddress inetAddress = EasyMock.mock(InetAddress.class); + EasyMock.replay(inetAddress); + PowerMock.mockStatic(InetAddress.class); + //noinspection ResultOfMethodCallIgnored + InetAddress.getLocalHost(); + PowerMock.expectLastCall().andReturn(inetAddress).times(times); + PowerMock.replay(InetAddress.class); + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java deleted file mode 100644 index 319bf19dcc..0000000000 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-core/src/test/java/org/openecomp/sdc/logging/context/HostAddressTest.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright © 2016-2018 European Support Limited - * - * 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. - */ - -package org.openecomp.sdc.logging.context; - -import static org.testng.Assert.assertNotNull; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import org.easymock.EasyMock; -import org.powermock.api.easymock.PowerMock; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.testng.PowerMockTestCase; -import org.testng.annotations.Test; - -/** - * Retrieval and caching of host address. - * - * @author evitaliy - * @since 28 Mar 2018 - */ -@PrepareForTest(InetAddress.class) -public class HostAddressTest extends PowerMockTestCase { - - @Test - public void hostAddressIsAlwaysPopulated() { - assertNotNull(new HostAddress().get()); - } - - @Test - public void cachedAddressRemainsTheSameWhenGotWithingRefreshInterval() throws UnknownHostException { - mockInetAddress(1); - HostAddress addressCache = new HostAddress(1000); - addressCache.get(); - addressCache.get(); - } - - @Test - public void cachedAddressReplacedWhenGotAfterRefreshInterval() throws UnknownHostException { - mockInetAddress(2); - HostAddress addressCache = new HostAddress(-1); - addressCache.get(); - addressCache.get(); - } - - private void mockInetAddress(int times) throws UnknownHostException { - InetAddress inetAddress = EasyMock.mock(InetAddress.class); - EasyMock.replay(inetAddress); - PowerMock.mockStatic(InetAddress.class); - //noinspection ResultOfMethodCallIgnored - InetAddress.getLocalHost(); - PowerMock.expectLastCall().andReturn(inetAddress).times(times); - PowerMock.replay(InetAddress.class); - } -} \ No newline at end of file -- cgit 1.2.3-korg