This is an automated email from the ASF dual-hosted git repository. cschneider pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git
commit 76a7d7606a478e9677c4ac3f2dc78619ce0a060e Author: Christian Schneider <cschn...@adobe.com> AuthorDate: Fri Apr 12 09:03:15 2024 +0200 SLING-12293 - Remove unused JcrBinaryStore --- .../journal/binary/jcr/JcrBinaryStore.java | 166 --------------------- .../journal/binary/jcr/PackageCleaner.java | 68 --------- .../journal/binary/jcr/PackageCleanupTask.java | 74 --------- .../journal/shared/DistributionMetricsService.java | 25 ---- .../journal/binary/jcr/JcrBinaryStoreTest.java | 134 ----------------- .../shared/DistributionMetricsServiceTest.java | 2 - 6 files changed, 469 deletions(-) diff --git a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStore.java b/src/main/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStore.java deleted file mode 100644 index 0369dc5..0000000 --- a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStore.java +++ /dev/null @@ -1,166 +0,0 @@ -/* - * 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 org.apache.sling.distribution.journal.binary.jcr; - -import static java.util.Collections.singletonMap; -import static org.apache.sling.api.resource.ResourceResolverFactory.SUBSERVICE; - -import java.io.IOException; -import java.io.InputStream; -import java.util.Objects; - -import javax.annotation.Nonnull; -import javax.jcr.Binary; -import javax.jcr.Node; -import javax.jcr.Property; -import javax.jcr.Session; -import javax.jcr.ValueFactory; -import javax.jcr.nodetype.NodeType; - -import org.apache.jackrabbit.api.ReferenceBinary; -import org.apache.jackrabbit.commons.JcrUtils; -import org.apache.jackrabbit.commons.jackrabbit.SimpleReferenceBinary; -import org.apache.sling.api.resource.LoginException; -import org.apache.sling.api.resource.PersistenceException; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ResourceResolver; -import org.apache.sling.api.resource.ResourceResolverFactory; -import org.apache.sling.api.resource.ResourceUtil; -import org.apache.sling.commons.metrics.Timer; -import org.apache.sling.distribution.common.DistributionException; -import org.apache.sling.distribution.journal.BinaryStore; -import org.apache.sling.distribution.journal.shared.DistributionMetricsService; -import org.apache.sling.serviceusermapping.ServiceUserMapped; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -@Component( - property = { - "type=jcr" - } -) -public class JcrBinaryStore implements BinaryStore { - private static final long MAX_INLINE_PKG_BINARY_SIZE = 800L * 1024; - private static final String SLING_FOLDER = "sling:Folder"; - static final String PACKAGES_ROOT_PATH = "/var/sling/distribution/journal/packagebinaries"; - - private static final Logger LOG = LoggerFactory.getLogger(JcrBinaryStore.class); - - @Reference - private ServiceUserMapped mapped; - - @Reference - private DistributionMetricsService distributionMetricsService; - - - @Reference - private ResourceResolverFactory resolverFactory; - - @Override public InputStream get(String reference) throws IOException { - try (ResourceResolver resolver = createResourceResolver()) { - Session session = resolver.adaptTo(Session.class); - if (session == null) { - throw new IOException("Unable to get Oak session"); - } - ValueFactory factory = session.getValueFactory(); - Binary binary = factory.createValue(new SimpleReferenceBinary(reference)).getBinary(); - return binary.getStream(); - } catch (Exception e) { - throw new IOException(e.getMessage(), e); - } - } - - @Override - public String put(String id, InputStream stream, long length) throws IOException { - if (length > MAX_INLINE_PKG_BINARY_SIZE) { - - /* - * Rather than pro-actively (and somewhat arbitrarily) - * decide to avoid sending a package inline based on - * its size, we could simply try to send packages of - * any size and only avoiding to inline as a fallback. - * However, this approach requires the messaging - * implementation to offer a mean to distinguish - * size issues when sending messages, which is not - * always the case. - */ - - LOG.info("Package {} too large ({}B) to be sent inline", id, length); - try { - return store(id, stream); - } catch (DistributionException e) { - throw new IOException(e.getMessage(), e); - } - } - return null; - } - - @Nonnull - public String store(String id, InputStream binaryStream)throws DistributionException { - try (ResourceResolver resolver = createResourceResolver()) { - String pkgPath = PACKAGES_ROOT_PATH + "/" + id; - Resource pkgResource = ResourceUtil.getOrCreateResource(resolver, - pkgPath, SLING_FOLDER, SLING_FOLDER, false); - Node pkgNode = Objects.requireNonNull(pkgResource.adaptTo(Node.class)); - Node binNode = JcrUtils.getOrAddNode(pkgNode, "bin", NodeType.NT_FILE); - Node cntNode = JcrUtils.getOrAddNode(binNode, Node.JCR_CONTENT, NodeType.NT_RESOURCE); - Binary binary = pkgNode.getSession().getValueFactory().createBinary(binaryStream); - cntNode.setProperty(Property.JCR_DATA, binary); - resolver.commit(); - String blobRef = ((ReferenceBinary) binary).getReference(); - LOG.info("Stored content package {} under path {} with blobRef {}", - id, pkgPath, blobRef); - return blobRef; - } catch (Exception e) { - throw new DistributionException(e.getMessage(), e); - } - } - - /** - * Delete all packages that are older than specified unix time - * @param deleteOlderThanTime - */ - public void cleanup(long deleteOlderThanTime) { - Timer.Context context = distributionMetricsService.getCleanupPackageDuration().time(); - // Auto-refresh policy is disabled for service resource resolver - try (ResourceResolver resolver = createResourceResolver()) { - - PackageCleaner packageCleaner = new PackageCleaner(resolver, deleteOlderThanTime); - Resource root = getRoot(resolver); - int removedCount = packageCleaner.cleanup(root); - distributionMetricsService.getCleanupPackageRemovedCount().increment(removedCount); - } catch (LoginException | PersistenceException e) { - throw new RuntimeException(e.getMessage(), e); - } finally { - context.stop(); - } - } - - @Nonnull - private Resource getRoot(ResourceResolver resolver) - throws PersistenceException { - return ResourceUtil.getOrCreateResource(resolver, PACKAGES_ROOT_PATH, SLING_FOLDER, SLING_FOLDER, true); - } - - private ResourceResolver createResourceResolver() throws LoginException { - return resolverFactory.getServiceResourceResolver(singletonMap(SUBSERVICE, "bookkeeper")); - } -} diff --git a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleaner.java b/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleaner.java deleted file mode 100644 index c706f8c..0000000 --- a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleaner.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 org.apache.sling.distribution.journal.binary.jcr; - -import org.apache.sling.api.resource.PersistenceException; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ResourceResolver; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class PackageCleaner { - private static final Logger LOG = LoggerFactory.getLogger(PackageCleaner.class); - - private ResourceResolver resolver; - private long deleteOlderThanTime; - - /** - * Delete all packages older than specified time - * - * @param resolver - * @param deleteOlderThanTime - */ - public PackageCleaner(ResourceResolver resolver, long deleteOlderThanTime) { - this.resolver = resolver; - this.deleteOlderThanTime = deleteOlderThanTime; - } - - public int cleanup(Resource root) - throws PersistenceException { - int removedCount = 0; - for (Resource pkgNode : root.getChildren()) { - removedCount += cleanNode(pkgNode); - } - if (resolver.hasChanges()) { - resolver.commit(); - } - return removedCount; - } - - private int cleanNode(Resource pkgNode) - throws PersistenceException { - long createdTime = pkgNode.getValueMap().get("jcr:created", Long.class); - if (createdTime < deleteOlderThanTime) { - LOG.info("removing package={}, created={} < deleteTime={}", pkgNode.getName(), createdTime, deleteOlderThanTime); - resolver.delete(pkgNode); - return 1; - } else { - return 0; - } - } - -} diff --git a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleanupTask.java b/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleanupTask.java deleted file mode 100644 index fbbfc96..0000000 --- a/src/main/java/org/apache/sling/distribution/journal/binary/jcr/PackageCleanupTask.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * 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 org.apache.sling.distribution.journal.binary.jcr; - -import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_CONCURRENT; -import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_IMMEDIATE; -import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_PERIOD; -import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_RUN_ON; -import static org.apache.sling.commons.scheduler.Scheduler.VALUE_RUN_ON_LEADER; - -import javax.annotation.ParametersAreNonnullByDefault; - -import org.apache.sling.distribution.journal.shared.PublisherConfigurationAvailable; -import org.osgi.service.component.annotations.Component; -import org.osgi.service.component.annotations.Reference; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * The cleanup task fetches the head offset (the smallest) and - * the tail offset (the current biggest) on the package topic. - * The stored packages are then scanned and cleaned if they are - * no longer referenced by the package topic (offset smaller than - * the head offset). - * With clustered deployment, only one Publisher agent should run - * the package cleanup task on the cluster - */ -@Component( - property = { - PROPERTY_SCHEDULER_CONCURRENT + ":Boolean=false", - PROPERTY_SCHEDULER_IMMEDIATE + ":Boolean=true", - PROPERTY_SCHEDULER_PERIOD + ":Long=" + 7 * 24 * 60 * 60, // 7 days - PROPERTY_SCHEDULER_RUN_ON + "=" + VALUE_RUN_ON_LEADER - }) -@ParametersAreNonnullByDefault -public class PackageCleanupTask implements Runnable { - - private static final Logger LOG = LoggerFactory.getLogger(PackageCleanupTask.class); - private static final long PKG_MAX_LIFETIME_MS = 30 * 24 * 60 * 60 * 1000L; - - @Reference - private JcrBinaryStore binaryStore; - - /** - * The task runs only when at least one DistributionSubscriber agent is configured. - */ - @Reference - private PublisherConfigurationAvailable publisherConfigurationAvailable; - - @Override - public void run() { - LOG.info("Starting Package Cleanup Task"); - long deleteOlderThanTime = System.currentTimeMillis() - PKG_MAX_LIFETIME_MS; - binaryStore.cleanup(deleteOlderThanTime); - LOG.info("Finished Package Cleanup Task"); - } - -} diff --git a/src/main/java/org/apache/sling/distribution/journal/shared/DistributionMetricsService.java b/src/main/java/org/apache/sling/distribution/journal/shared/DistributionMetricsService.java index 0786f13..c77e37f 100644 --- a/src/main/java/org/apache/sling/distribution/journal/shared/DistributionMetricsService.java +++ b/src/main/java/org/apache/sling/distribution/journal/shared/DistributionMetricsService.java @@ -44,10 +44,6 @@ public class DistributionMetricsService { private final MetricsService metricsService; - private final Counter cleanupPackageRemovedCount; - - private final Timer cleanupPackageDuration; - private final Histogram importedPackageSize; private final Histogram exportedPackageSize; @@ -101,8 +97,6 @@ public class DistributionMetricsService { @Activate public DistributionMetricsService(@Reference MetricsService metricsService) { this.metricsService = metricsService; - cleanupPackageRemovedCount = getCounter(getMetricName(PUB_COMPONENT, "cleanup_package_removed_count")); - cleanupPackageDuration = getTimer(getMetricName(PUB_COMPONENT, "cleanup_package_duration")); exportedPackageSize = getHistogram(getMetricName(PUB_COMPONENT, "exported_package_size")); acceptedRequests = getMeter(getMetricName(PUB_COMPONENT, "accepted_requests")); droppedRequests = getMeter(getMetricName(PUB_COMPONENT, "dropped_requests")); @@ -161,25 +155,6 @@ public class DistributionMetricsService { } } - /** - * Counter of package removed during the Package Cleanup Task. - * The count is the sum of all packages removed since the service started. - * - * @return a Sling Metrics timer - */ - public Counter getCleanupPackageRemovedCount() { - return cleanupPackageRemovedCount; - } - - /** - * Timer of the Package Cleanup Task execution duration. - * - * @return a Sling Metrics timer - */ - public Timer getCleanupPackageDuration() { - return cleanupPackageDuration; - } - /** * Histogram of the imported content package size in Byte. * diff --git a/src/test/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStoreTest.java b/src/test/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStoreTest.java deleted file mode 100644 index 15cfcde..0000000 --- a/src/test/java/org/apache/sling/distribution/journal/binary/jcr/JcrBinaryStoreTest.java +++ /dev/null @@ -1,134 +0,0 @@ -/* - * 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 org.apache.sling.distribution.journal.binary.jcr; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.equalTo; -import static org.mockito.Mockito.when; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -import org.apache.sling.api.resource.LoginException; -import org.apache.sling.api.resource.Resource; -import org.apache.sling.api.resource.ResourceResolver; -import org.apache.sling.api.resource.ResourceResolverFactory; -import org.apache.sling.commons.metrics.Counter; -import org.apache.sling.commons.metrics.Timer; -import org.apache.sling.distribution.common.DistributionException; -import org.apache.sling.distribution.journal.MessagingProvider; -import org.apache.sling.distribution.journal.messages.PackageMessage; -import org.apache.sling.distribution.journal.shared.DistributionMetricsService; -import org.apache.sling.distribution.journal.shared.Topics; -import org.apache.sling.testing.mock.osgi.MockOsgi; -import org.apache.sling.testing.mock.sling.MockSling; -import org.apache.sling.testing.mock.sling.ResourceResolverType; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; -import org.mockito.Captor; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.Spy; -import org.osgi.framework.BundleContext; - -public class JcrBinaryStoreTest { - - @Spy - private BundleContext bundleContext = MockOsgi.newBundleContext(); - - @Spy - ResourceResolverFactory resolverFactory = MockSling.newResourceResolverFactory(ResourceResolverType.JCR_OAK, bundleContext); - - @Mock - private MessagingProvider messagingProvider; - - @Mock - private Timer timer; - - @Mock - private Timer.Context context; - - @Mock - private Counter counter; - - @Mock - private DistributionMetricsService distributionMetricsService; - - @Captor - private ArgumentCaptor<PackageMessage> pkgCaptor; - - @Spy - private Topics topics = new Topics(); - - @InjectMocks - private JcrBinaryStore packageRepo; - - @Before - public void before() throws Exception { - MockitoAnnotations.openMocks(this).close(); - } - - @After - public void after() { - } - - @Test - public void testStoreClean() throws DistributionException, IOException, LoginException, InterruptedException { - when(timer.time()) - .thenReturn(context); - when(distributionMetricsService.getCleanupPackageDuration()) - .thenReturn(timer); - when(distributionMetricsService.getCleanupPackageRemovedCount()) - .thenReturn(counter); - - long createTime = System.currentTimeMillis(); - String id = UUID.randomUUID().toString(); - byte[] content = new byte[] {}; - InputStream binaryStream = new ByteArrayInputStream(content); - packageRepo.store(id, binaryStream); - assertNumNodes(1); - packageRepo.cleanup(createTime - 1000); - assertNumNodes(1); - packageRepo.cleanup(createTime + 1000); - assertNumNodes(0); - } - - private void assertNumNodes(int num) throws LoginException { - try (ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null)) { - assertThat(getPackageNodes(resolver).size(), equalTo(num)); - } - } - - private List<Resource> getPackageNodes(ResourceResolver resolver) throws LoginException { - List<Resource> result = new ArrayList<>(); - Resource root = resolver.getResource(JcrBinaryStore.PACKAGES_ROOT_PATH); - for (Resource pkg : root.getChildren()) { - result.add(pkg); - } - return result; - } - -} diff --git a/src/test/java/org/apache/sling/distribution/journal/shared/DistributionMetricsServiceTest.java b/src/test/java/org/apache/sling/distribution/journal/shared/DistributionMetricsServiceTest.java index f4bdc02..4bca99e 100644 --- a/src/test/java/org/apache/sling/distribution/journal/shared/DistributionMetricsServiceTest.java +++ b/src/test/java/org/apache/sling/distribution/journal/shared/DistributionMetricsServiceTest.java @@ -57,8 +57,6 @@ public class DistributionMetricsServiceTest { public void testGetMetrics() { assertNotNull(metrics.getAcceptedRequests()); assertNotNull(metrics.getBuildPackageDuration()); - assertNotNull(metrics.getCleanupPackageDuration()); - assertNotNull(metrics.getCleanupPackageRemovedCount()); assertNotNull(metrics.getDroppedRequests()); assertNotNull(metrics.getEnqueuePackageDuration()); assertNotNull(metrics.getExportedPackageSize());