com.gemstone.gemfire.distributed.internal.deadlock.MessageDependencyMonitor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gemfire-core Show documentation
Show all versions of gemfire-core Show documentation
SnappyData store based off Pivotal GemFireXD
The newest version!
/*
* Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
*
* Licensed 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. See accompanying
* LICENSE file.
*/
package com.gemstone.gemfire.distributed.internal.deadlock;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import com.gemstone.gemfire.distributed.internal.DistributionMessage;
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
import com.gemstone.gemfire.distributed.internal.MessageWithReply;
import com.gemstone.gemfire.distributed.internal.ReplyProcessor21;
import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
/**
*
* This class keeps track of the information we need
* to determine which threads in the receiver system a reply
* processor thread is waiting for.
*
* This will allow us to programatically discover deadlocks.
* @author dsmith
*
*/
public class MessageDependencyMonitor implements DependencyMonitor {
private final UnsafeThreadLocal waitingProcessors = new UnsafeThreadLocal();
private final UnsafeThreadLocal processingMessages= new UnsafeThreadLocal();
public static final MessageDependencyMonitor INSTANCE;
static {
INSTANCE = new MessageDependencyMonitor();
DependencyMonitorManager.addMonitor(INSTANCE);
}
public static void waitingForReply(ReplyProcessor21 reply) {
INSTANCE.waitingProcessors.set(reply);
}
public static void doneWaiting(ReplyProcessor21 reply) {
INSTANCE.waitingProcessors.set(null);
}
public static void processingMessage(DistributionMessage message) {
if(message instanceof MessageWithReply) {
INSTANCE.processingMessages.set((MessageWithReply) message);
}
}
public static void doneProcessing(DistributionMessage message) {
if(message instanceof MessageWithReply) {
INSTANCE.processingMessages.set(null);
}
}
public Set> getBlockedThreads(Thread[] allThreads) {
InternalDistributedSystem system = InternalDistributedSystem.getAnyInstance();
if(system == null) {
return Collections.emptySet();
}
InternalDistributedMember myId = system.getDistributedMember();
Set> blockedThreads = new HashSet>();
for(Thread thread : allThreads) {
ReplyProcessor21 processor = waitingProcessors.get(thread);
if(processor != null && processor.getProcessorId() > 0) {
blockedThreads.add(new Dependency(thread, new MessageKey(myId, processor.getProcessorId())));
}
}
return blockedThreads;
}
public Set> getHeldResources(Thread[] allThreads) {
Set> heldResources = new HashSet>();
for(Thread thread : allThreads) {
MessageWithReply message = processingMessages.get(thread);
if(message != null && message.getProcessorId() > 0) {
heldResources.add(new Dependency(new MessageKey(message.getSender(), message.getProcessorId()), thread));
}
}
return heldResources;
}
private static class MessageKey implements Serializable {
private final InternalDistributedMember myId;
private final int processorId;
public MessageKey(InternalDistributedMember myId, int processorId) {
super();
this.myId = myId;
this.processorId = processorId;
}
public InternalDistributedMember getMyId() {
return myId;
}
public int getProcessorId() {
return processorId;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((myId == null) ? 0 : myId.hashCode());
result = prime * result + processorId;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof MessageKey))
return false;
MessageKey other = (MessageKey) obj;
if (myId == null) {
if (other.myId != null)
return false;
} else if (!myId.equals(other.myId))
return false;
if (processorId != other.processorId)
return false;
return true;
}
@Override
public String toString() {
return "MessageFrom(" + myId + ", " + processorId + ")";
}
}
}