cameronlee314 commented on a change in pull request #1532:
URL: https://github.com/apache/samza/pull/1532#discussion_r715181067



##########
File path: 
samza-log4j2/src/main/java/org/apache/samza/logging/log4j2/StreamAppender.java
##########
@@ -152,18 +144,19 @@ public String getStreamName() {
   }
 
   /**
-   * Getter for the Config parameter.
+   * This should only be called after verifying that the {@link 
LoggingContextHolder} has the config.

Review comment:
       This usage pattern constraint actually already exists in the current 
implementation. In the AM, it is already not guaranteed that the config is 
available. I was just adding some documentation to try to help clarify that now.
   Unfortunately, to improve this, we would need to change the semantics of 
some other parts of this class. I actually did make some other changes in the 
initial iteration, but @lakshmi-manasa-g made a good point about maintaining 
compatibility within the scope of this PR. There is more discussion of this at 
https://github.com/apache/samza/pull/1532#discussion_r714217467.
   Ideally, there would have been one fetch to the config and then that would 
have been passed around as a local argument instead of through an instance 
variable. That would have allowed better enforcement of the availability of the 
config. For now, this continues to need to be manually checked for ordering of 
method calls.

##########
File path: 
samza-core/src/main/java/org/apache/samza/logging/LoggingContextHolder.java
##########
@@ -0,0 +1,56 @@
+/*
+ * 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.samza.logging;
+
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.samza.config.Config;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * Holds information to be used by loggers. For example, some custom Samza 
log4j/log4j2 logging appenders need system
+ * configs for initialization, so this allows the configs to be passed to 
those appenders.
+ */
+public class LoggingContextHolder {
+  private static final Logger LOG = 
LoggerFactory.getLogger(LoggingContextHolder.class);
+  public static final LoggingContextHolder INSTANCE = new 
LoggingContextHolder();
+
+  private final AtomicReference<Config> config = new AtomicReference<>();
+
+  @VisibleForTesting
+  LoggingContextHolder() {
+  }

Review comment:
       Whenever possible, I prefer to avoid using statics in tests. If the 
statics are not properly reset (and it's not always obvious that a static needs 
to be reset unless you look at the class), then other tests could be impacted, 
and that can lead to hard-to-debug failures. For example, a test using a static 
might pass when run on its own, but then if it runs as part of a full build, it 
might fail since the static wasn't reset properly from some other test.
   I don't think it is too much of a problem that we can't exactly strictly 
enforce the expected usage. Package-private gets us pretty far, and even if 
someone abuses the package privacy and uses their own `LoggingContextHolder`, 
there isn't really anything useful to get out of that. Also, 
`@VisibleForTesting` documents this as "test only".




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to