|
| 1 | +/* |
| 2 | + * Copyright 2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.elasticsearch.support; |
| 17 | + |
| 18 | +import java.io.InputStream; |
| 19 | +import java.util.Properties; |
| 20 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 21 | + |
| 22 | +import org.elasticsearch.Version; |
| 23 | +import org.slf4j.Logger; |
| 24 | +import org.slf4j.LoggerFactory; |
| 25 | +import org.springframework.lang.Nullable; |
| 26 | + |
| 27 | +/** |
| 28 | + * This class is used to log the versions of Spring Data Elasticsearch, the Elasticsearch client libs used to built, the |
| 29 | + * Elasticsearch client libs currently used and of the Elasticsearch cluster. If differences greater than a patchlevel |
| 30 | + * are detected, these are logged as warnings. |
| 31 | + * |
| 32 | + * @author Peter-Josef Meisch |
| 33 | + * @since 4.0 |
| 34 | + */ |
| 35 | +public final class VersionInfo { |
| 36 | + |
| 37 | + private static final Logger LOG = LoggerFactory.getLogger(VersionInfo.class); |
| 38 | + private static final AtomicBoolean initialized = new AtomicBoolean(false); |
| 39 | + private static String VERSION_PROPERTIES = "versions.properties"; |
| 40 | + |
| 41 | + /** |
| 42 | + * logs the relevant version info the first time it is called. Does nothing after the first call |
| 43 | + * |
| 44 | + * @param clusterVersion the version of the cluster |
| 45 | + */ |
| 46 | + public static void logVersions(@Nullable String clusterVersion) { |
| 47 | + if (!initialized.getAndSet(true)) { |
| 48 | + try { |
| 49 | + InputStream resource = VersionInfo.class.getClassLoader().getResourceAsStream(VERSION_PROPERTIES); |
| 50 | + if (resource != null) { |
| 51 | + Properties properties = new Properties(); |
| 52 | + properties.load(resource); |
| 53 | + |
| 54 | + String versionSpringDataElasticsearch = properties.getProperty("version.spring-data-elasticsearch"); |
| 55 | + Version versionESBuilt = Version.fromString(properties.getProperty("version.elasticsearch-client")); |
| 56 | + Version versionESUsed = Version.CURRENT; |
| 57 | + Version versionESCluster = clusterVersion != null ? Version.fromString(clusterVersion) : null; |
| 58 | + |
| 59 | + LOG.info("Version Spring Data Elasticsearch: {}", versionSpringDataElasticsearch.toString()); |
| 60 | + LOG.info("Version Elasticsearch Client in build: {}", versionESBuilt.toString()); |
| 61 | + LOG.info("Version Elasticsearch Client used: {}", versionESUsed.toString()); |
| 62 | + |
| 63 | + if (differInMajorOrMinor(versionESBuilt, versionESUsed)) { |
| 64 | + LOG.warn("Version mismatch in between Elasticsearch Clients build/use: {} - {}", versionESBuilt, |
| 65 | + versionESUsed); |
| 66 | + } |
| 67 | + |
| 68 | + if (versionESCluster != null) { |
| 69 | + LOG.info("Version Elasticsearch cluster: {}", versionESCluster.toString()); |
| 70 | + |
| 71 | + if (differInMajorOrMinor(versionESUsed, versionESCluster)) { |
| 72 | + LOG.warn("Version mismatch in between Elasticsearch Client and Cluster: {} - {}", versionESUsed, |
| 73 | + versionESCluster); |
| 74 | + } |
| 75 | + } |
| 76 | + } else { |
| 77 | + LOG.warn("cannot load {}", VERSION_PROPERTIES); |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + LOG.warn("Could not log version info: {} - {}", e.getClass().getSimpleName(), e.getMessage()); |
| 81 | + } |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private static boolean differInMajorOrMinor(Version version1, Version version2) { |
| 87 | + return version1.major != version2.major || version1.minor != version2.minor; |
| 88 | + } |
| 89 | + |
| 90 | + private VersionInfo() {} |
| 91 | +} |
0 commit comments