Skip to content

Issue 1747 mongodb4 long collection size configuration #1749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class MongoDb4Connection extends AbstractNoSqlConnection<Document,
private static final Logger LOGGER = StatusLogger.getLogger();

private static MongoCollection<Document> getOrCreateMongoCollection(final MongoDatabase database,
final String collectionName, final boolean isCapped, final Integer sizeInBytes) {
final String collectionName, final boolean isCapped, final Long sizeInBytes) {
try {
LOGGER.debug("Gettting collection '{}'...", collectionName);
// throws IllegalArgumentException if collectionName is invalid
Expand All @@ -66,6 +66,11 @@ private static MongoCollection<Document> getOrCreateMongoCollection(final MongoD

public MongoDb4Connection(final ConnectionString connectionString, final MongoClient mongoClient,
final MongoDatabase mongoDatabase, final boolean isCapped, final Integer sizeInBytes) {
this(connectionString, mongoClient, mongoDatabase, isCapped, Long.valueOf(sizeInBytes));
}

public MongoDb4Connection(final ConnectionString connectionString, final MongoClient mongoClient,
final MongoDatabase mongoDatabase, final boolean isCapped, final Long sizeInBytes) {
this.connectionString = connectionString;
this.mongoClient = mongoClient;
this.collection = getOrCreateMongoCollection(mongoDatabase, connectionString.getCollection(), isCapped,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static class Builder<B extends Builder<B>> extends AbstractFilterable.Bui
private String connectionStringSource;

@PluginBuilderAttribute
private int collectionSize = DEFAULT_COLLECTION_SIZE;
private long collectionSize = DEFAULT_COLLECTION_SIZE;

@PluginBuilderAttribute("capped")
private boolean capped = false;
Expand All @@ -72,7 +72,12 @@ public B setCollectionSize(final int collectionSize) {
this.collectionSize = collectionSize;
return asBuilder();
}
}

public B setCollectionSize(final long collectionSize) {
this.collectionSize = collectionSize;
return asBuilder();
}
}

private static final Logger LOGGER = StatusLogger.getLogger();

Expand All @@ -84,21 +89,21 @@ public B setCollectionSize(final int collectionSize) {
// @formatter:on

// TODO Where does this number come from?
private static final int DEFAULT_COLLECTION_SIZE = 536_870_912;
private static final long DEFAULT_COLLECTION_SIZE = 536_870_912;

@PluginBuilderFactory
public static <B extends Builder<B>> B newBuilder() {
return new Builder<B>().asBuilder();
}

private final Integer collectionSize;
private final Long collectionSize;
private final boolean isCapped;
private final MongoClient mongoClient;
private final MongoDatabase mongoDatabase;
private final ConnectionString connectionString;

private MongoDb4Provider(final String connectionStringSource, final boolean isCapped,
final Integer collectionSize) {
final Long collectionSize) {
LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
this.connectionString = new ConnectionString(connectionStringSource);
LOGGER.debug("Created ConnectionString {}", connectionString);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@
import com.mongodb.client.MongoDatabase;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.bson.Document;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@UsingMongoDb4
@LoggerContextSource("log4j2-mongodb-capped.xml")
public class MongoDb4CappedTest {
public abstract class AbstractMongoDb4CappedTest {

@Test
public void test(final LoggerContext ctx, final MongoClient mongoClient) {
final Logger logger = ctx.getLogger(MongoDb4CappedTest.class);
final Logger logger = ctx.getLogger(AbstractMongoDb4CappedTest.class);
logger.info("Hello log");
final MongoDatabase database = mongoClient.getDatabase(MongoDb4TestConstants.DATABASE_NAME);
Assertions.assertNotNull(database);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.logging.log4j.mongodb4;

import org.apache.logging.log4j.core.test.junit.LoggerContextSource;

@UsingMongoDb4
@LoggerContextSource("log4j2-mongodb-capped-int.xml")
public class MongoDb4CappedIntTest extends AbstractMongoDb4CappedTest {

// test is in superclass
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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.logging.log4j.mongodb4;

import org.apache.logging.log4j.core.test.junit.LoggerContextSource;

@UsingMongoDb4
@LoggerContextSource("log4j2-mongodb-capped-long.xml")
public class MongoDb4CappedLongTest extends AbstractMongoDb4CappedTest {

// test is in superclass
}
33 changes: 33 additions & 0 deletions log4j-mongodb4/src/test/resources/log4j2-mongodb-capped-long.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<Configuration status="WARN">
<Appenders>
<NoSql name="MongoDbAppender">
<!-- collectionSize="2147483657" is max int + 10 -->
<MongoDb4
connection="mongodb://localhost:${test:MongoDBTestPort:-27017}/testDb.testCollection"
capped="true"
collectionSize="2147483657"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
<AppenderRef ref="MongoDbAppender" />
</Root>
</Loggers>
</Configuration>
28 changes: 28 additions & 0 deletions src/changelog/.2.x.x/1747_mongodb4-long-collection-size-conifg.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://logging.apache.org/log4j/changelog"
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.0.xsd"
type="fixed">
<issue id="1747" link="https://github.com/apache/logging-log4j2/issues/1747"/>
<author name="Gary Gregory" id="github:garydgregory"/>
<author name=" Jordi Font" id="github:jfont-teg"/>
<description format="asciidoc">
The MongoDb4 appender now supports long values to configure `collectionSize`
</description>
</entry>
2 changes: 1 addition & 1 deletion src/site/xdoc/manual/appenders.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2550,7 +2550,7 @@ public class JpaLogEntity extends AbstractLogEventWrapperEntity {
</tr>
<tr>
<td>collectionSize</td>
<td>int</td>
<td>long</td>
<td>Specify the size in bytes of the capped collection to use if enabled. The minimum size is 4096 bytes,
and larger sizes will be increased to the nearest integer multiple of 256. See the capped collection documentation
linked above for more information.</td>
Expand Down