Skip to content

Make sure domainUid is in operator log messages - part 1 #1844

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 7 commits into from
Aug 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add domainUID to watcher related log messages
  • Loading branch information
doxiao committed Jul 30, 2020
commit 45daf6d2ca4ea8c2cfad920b7bb6e1e6c8446d9a
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1ConfigMap;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -64,4 +66,10 @@ public WatchI<V1ConfigMap> initiateWatch(WatchBuilder watchBuilder) throws ApiEx
public String getNamespace() {
return ns;
}

@Override
public String getDomainUID(Response<V1ConfigMap> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1ConfigMap::getMetadata).orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,8 +606,18 @@ public void execute() {
if (!delegate.isNamespaceRunning(getNamespace())) {
return;
}

Packet packet = new Packet();
packet
.getComponents()
.put(
ProcessingConstants.DOMAIN_COMPONENT_NAME,
Component.createFor(liveInfo, delegate.getVersion(),
PodAwaiterStepFactory.class, delegate.getPodAwaiterStepFactory(getNamespace()),
V1SubjectRulesReviewStatus.class, delegate.getSubjectRulesReviewStatus(getNamespace())));

if (isShouldContinue()) {
internalMakeRightDomainPresence();
internalMakeRightDomainPresence(packet);
} else {
LOGGER.fine(MessageKeys.NOT_STARTING_DOMAINUID_THREAD, getDomainUid());
}
Expand Down Expand Up @@ -636,19 +646,10 @@ private boolean isShouldContinue() {
return false;
}

private void internalMakeRightDomainPresence() {
private void internalMakeRightDomainPresence(Packet packet) {
LOGGER.fine(MessageKeys.PROCESSING_DOMAIN, getDomainUid());

Packet packet = new Packet();
packet.put(MAKE_RIGHT_DOMAIN_OPERATION, this);
packet
.getComponents()
.put(
ProcessingConstants.DOMAIN_COMPONENT_NAME,
Component.createFor(liveInfo, delegate.getVersion(),
PodAwaiterStepFactory.class, delegate.getPodAwaiterStepFactory(getNamespace()),
V1SubjectRulesReviewStatus.class, delegate.getSubjectRulesReviewStatus(getNamespace())));

runDomainPlan(
getDomain(),
getDomainUid(),
Expand Down Expand Up @@ -748,7 +749,8 @@ public void onThrowable(Packet packet, Throwable throwable) {
() -> {
DomainPresenceInfo existing = getExistingDomainPresenceInfo(ns, domainUid);
if (existing != null) {
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(ns)) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(ns).domainUid(domainUid)) {
existing.setPopulated(false);
// proceed only if we have not already retried max number of times
int retryCount = existing.incrementAndGetFailureCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -62,4 +64,9 @@ public WatchI<Domain> initiateWatch(WatchBuilder watchBuilder) throws ApiExcepti
public String getNamespace() {
return ns;
}

@Override
public String getDomainUID(Response<Domain> item) {
return Optional.ofNullable(item.object).map(Domain::getDomainUid).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Event;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -67,4 +69,10 @@ public WatchI<V1Event> initiateWatch(WatchBuilder watchBuilder) throws ApiExcept
public String getNamespace() {
return ns;
}

@Override
public String getDomainUID(Response<V1Event> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1Event::getMetadata).orElse(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public String getNamespace() {
return namespace;
}

@Override
public String getDomainUID(Watch.Response<V1Job> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1Job::getMetadata).orElse(null));
}

/**
* Creates a new JobWatcher and caches it by namespace.
*
Expand Down
4 changes: 3 additions & 1 deletion operator/src/main/java/oracle/kubernetes/operator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,9 @@ public NextAction onSuccess(Packet packet, CallResponse<V1NamespaceList> callRes
Set<String> namespacesToStart = new TreeSet<>(targetNamespaces);
for (String ns : targetNamespaces) {
if (!nsList.contains(ns)) {
LOGGER.warning(MessageKeys.NAMESPACE_IS_MISSING, ns);
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(ns)) {
LOGGER.warning(MessageKeys.NAMESPACE_IS_MISSING, ns);
}
namespacesToStart.remove(ns);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Namespace;
import io.kubernetes.client.util.Watch;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -59,4 +60,9 @@ public WatchI<V1Namespace> initiateWatch(WatchBuilder watchBuilder) throws ApiEx
public String getNamespace() {
return null;
}

@Override
public String getDomainUID(Watch.Response<V1Namespace> item) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ public String getNamespace() {
return namespace;
}

@Override
public String getDomainUID(Watch.Response<V1Pod> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1Pod::getMetadata).orElse(null));
}

/**
* Receive response.
* @param item item
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ public NextAction apply(Packet packet) {
String state = null;
ClientPool helper = ClientPool.getInstance();
ApiClient client = helper.take();
try (LoggingContext stack = LoggingContext.setThreadContext().namespace(getNamespace(pod))) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace(pod)).domainUid(getDomainUID(pod))) {
try {
KubernetesExec kubernetesExec = EXEC_FACTORY.create(client, pod, CONTAINER_NAME);
kubernetesExec.setStdin(stdin);
Expand Down Expand Up @@ -216,6 +217,14 @@ private String getNamespace(@Nonnull V1Pod pod) {
return Optional.ofNullable(pod.getMetadata()).map(V1ObjectMeta::getNamespace).orElse(null);
}

public String getDomainUID(V1Pod pod) {
String key = "weblogic.domainUID";
return Optional.of(pod)
.map(V1Pod::getMetadata)
.map(V1ObjectMeta::getLabels)
.map(labels -> labels.get(key)).orElse(null);
}

private String chooseStateOrLastKnownServerStatus(
LastKnownStatus lastKnownStatus, String state) {
if (state != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@

package oracle.kubernetes.operator;

import java.util.Optional;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicBoolean;

import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.models.V1Service;
import io.kubernetes.client.util.Watch.Response;
import oracle.kubernetes.operator.TuningParameters.WatchTuning;
import oracle.kubernetes.operator.builders.WatchBuilder;
import oracle.kubernetes.operator.builders.WatchI;
Expand Down Expand Up @@ -64,4 +66,10 @@ public WatchI<V1Service> initiateWatch(WatchBuilder watchBuilder) throws ApiExce
public String getNamespace() {
return ns;
}

@Override
public String getDomainUID(Response<V1Service> item) {
return getDomainUID(Optional.ofNullable(item.object)
.map(V1Service::getMetadata).orElse(null));
}
}
18 changes: 17 additions & 1 deletion operator/src/main/java/oracle/kubernetes/operator/Watcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ private void watchForEvents() {
continue;
}

try (LoggingContext stack = LoggingContext.setThreadContext().namespace(getNamespace())) {
try (LoggingContext stack =
LoggingContext.setThreadContext().namespace(getNamespace()).domainUid(getDomainUID(item))) {
if (isError(item)) {
handleErrorResponse(item);
} else {
Expand Down Expand Up @@ -195,6 +196,21 @@ private boolean hasNext(WatchI<T> watch) {
*/
public abstract String getNamespace();

/**
* Gets the domain identifier associated with the watcher.
*
* @return String object or null if the watcher is not associated with a domain
*/
public abstract String getDomainUID(Watch.Response<T> item);

protected String getDomainUID(V1ObjectMeta metadata) {
String key = "weblogic.domainUID";
return Optional.ofNullable(metadata)
.map(V1ObjectMeta::getLabels)
.map(labels -> labels.get(key))
.orElse(null);
}

private boolean isError(Watch.Response<T> item) {
return item.type.equalsIgnoreCase("ERROR");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,20 @@ private String getDomainUid(Fiber fiber) {
.map(Fiber::getPacket)
.map(this::getDomainPresenceInfo)
.map(DomainPresenceInfo::getDomainUid)
.orElse(getDomainUidFromThreadContext());
.orElse(getDomainUidFromLoggingContext(fiber));
}

private DomainPresenceInfo getDomainPresenceInfo(Packet packet) {
return packet.getSpi(DomainPresenceInfo.class);
private String getDomainUidFromLoggingContext(Fiber fiber) {
return Optional.ofNullable(fiber)
.map(Fiber::getPacket)
.map(p -> p.getSpi(LoggingContext.class))
.or(LoggingContext::optionalContext)
.map(LoggingContext::domainUid)
.orElse("");
}

private String getDomainUidFromThreadContext() {
return LoggingContext.optionalContext().map(LoggingContext::domainUid).orElse("");
private DomainPresenceInfo getDomainPresenceInfo(Packet packet) {
return packet.getSpi(DomainPresenceInfo.class);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public void whenPacketContainsDomainPresence_retrieveDomainUid() {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-uid"));
}

@Test
public void whenPacketContainsLoggingContext_retrieveDomainUid() {
testSupport.addLoggingContext(new LoggingContext().domainUid("test-lc-uid"));

assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-uid"));
}

@Test
public void whenNotInFiber_retrieveDomainUidFromThread() throws JsonProcessingException {
Expand All @@ -96,6 +102,29 @@ public void whenNotInFiber_retrieveDomainUidFromThread() throws JsonProcessingEx
}
}

@Test
public void whenPacketContainsDomainPresenceAndLoggingContext_retrieveDomainUidFromDomainPresence() {
testSupport.addDomainPresenceInfo(new DomainPresenceInfo("test-ns", "test-uid"));
testSupport.addLoggingContext(new LoggingContext().namespace("test-lc-ns").domainUid("test-lc-uid"));

assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-uid"));
}

@Test
public void whenPacketContainsLoggingContextAndThreadLocalIsDefined_retrieveDomainUidFromLoggingContext() {
testSupport.addLoggingContext(new LoggingContext().domainUid("test-lc-uid1"));
try (LoggingContext stack = LoggingContext.setThreadContext().namespace("test-lc-tl-uid")) {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-uid1"));
}
}

@Test
public void whenThreadLocalDefinedAndPacketContainsNoDomainPresenceOrLoggingContext_retrieveDomainUidFromThread() {
try (LoggingContext stack = LoggingContext.setThreadContext().domainUid("test-lc-tl-uid1")) {
assertThat(getFormattedMessageInFiber().get("domainUID"), equalTo("test-lc-tl-uid1"));
}
}

@Test
public void whenPacketLacksDomainPresence_domainNamespaceIsEmpty() {
assertThat(getFormattedMessageInFiber().get("namespace"), equalTo(""));
Expand Down