ozone core API

org.ozoneDB.core
Class GarbageCollector

java.lang.Object
  extended byorg.ozoneDB.core.ServerComponent
      extended byorg.ozoneDB.core.GarbageCollector
All Implemented Interfaces:
java.lang.Runnable

public class GarbageCollector
extends ServerComponent
implements java.lang.Runnable

Marks reachable objects and sweeps unreachable objects.

This implementation has at least following deficiencies:

Author:
Xu?n Baldauf (Medium.net

Nested Class Summary
 class GarbageCollector.GarbageCollectorProxyObjectIdentificationObjectOutputStream
          ObjectOutputStream which servers as an intra ObjectContainer object-graph-walker to detect all OzoneProxy instances an OzoneObject does refer.
 
Field Summary
protected  int actionsWithinTransactionCount
          The count of actions which were done within this garbageCollector.
protected  int currentGarbageCollectionLevel
          The number which represents the current garbage collection run.
protected  int doneReachableGarbageCollectionLevel
          The garbageCollectionLevel which objects have if they belong to the doneReachable set.
protected  java.lang.Object garbageCollectionLevelsLock
          This is the lock that everybody has to be synchronized to in order to be able to access any garbageCollectionLevel in any object within this database.
protected  java.lang.Thread garbageCollectionThread
          The thread which actually does the work.
protected  boolean kill
          Wether we should stop running as soon as possible.
protected  int phase
          The current phase this GarbageCollector is in.
protected static int PHASE_IDLE
          Doing nothing
protected static int PHASE_MARKING
          We are started, we are marking all reachable objects
protected static int PHASE_MARKING2
          We are started, we are marking all reachable objects
protected static int PHASE_READY_TO_START
          We may start, all old transactions are complete
protected static int PHASE_RUN_INITIATED
          A run has been initiated
protected static int PHASE_SWEEPING
          We are started, we are sweeping all unreachable objects
protected static int PHASE_WAITING_FOR_NEW_TRANSACTIONS_TO_COMPLETE
          Waiting for the old transactions to complete
protected static int PHASE_WAITING_FOR_OLD_TRANSACTIONS_TO_COMPLETE
          Waiting for the old transactions to complete
protected  org.ozoneDB.data.SimpleArrayList surelyReachableObjectsWhichHaveToBeMarkedAsSuch
          A stack of ObjectIDs of objects, which are surely reachable, but still were not marked as such.
protected  java.util.LinkedList surelyReachableObjectsWhichShouldHaveBeenProcessedButWereLockContented
          The list of ObjectIDs of objects which are surelyReachable and already were tried to be processed but where locking the objects failed.
protected  Transaction transaction
          The current transaction of this garbageCollector.
protected  java.util.HashSet transactionsRequiredToComplete
          The list of Transactions, which have to be completed before the GarbageCollector may start.
 
Fields inherited from class org.ozoneDB.core.ServerComponent
env
 
Constructor Summary
protected GarbageCollector(Env env)
          Creates a new garbage collector.
 
Method Summary
protected  void addAllNamedObjectsToSurelyReachableSet()
          This method walks through all named objects and adds each of them to the surelyReachable set.
protected  void addObjectIDToSurelyReachableObjectsWhichHaveToBeMarkedAsSuch(ObjectID id)
          This method may be called both from transaction threads and from GarbageCollector-Threads.
protected  void addRootSetElementsToSurelyReachableSet()
           
 void addTransactionRequiredToComplete(java.lang.Object ta)
           
protected  void checkForEndOfWaitForCurrentTransactionsToCompletePhase()
           
protected  void checkForProxyBorderCross(java.lang.Object o)
          This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys.
protected  void checkForProxyBorderCross(org.ozoneDB.OzoneCompatible o)
          This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys.
protected  void checkForProxyBorderCross(org.ozoneDB.OzoneProxy o)
          This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys.
protected  void completeTransaction()
          Completes the current transaction and releases it.
protected  void deferProcessingOfObjectContainerDueToLockContention(ObjectContainer objectContainer)
          Defers the processing of the given objectContainer because locking was not possible at current time.
protected  void ensureDoneReachable(ObjectContainer objectContainer)
          Ensures that the given object is doneReachable.
protected  void ensureSurelyReachable(ObjectContainer objectContainer)
          Calling this method makes sure that the object which is referenced by the given ID is at least surely reachable.
protected  void ensureSurelyReachable(ObjectID id)
          Calling this method makes sure that the object which is referenced by the given ID is at least surely reachable.
protected  void incCurrentGarbageCollectorLevel()
           
 void interceptInvocationPost(Transaction transaction, ObjectContainer callee, java.lang.Object result)
          This method is called by Transactions to indicate that a method of the object callee was called by the transaction.
 void interceptInvocationPre(Transaction transaction, ObjectContainer callee, java.lang.Object[] args)
          This method is called by Transactions to indicate that a method of the object callee will be called by the transaction.
protected  void internalAbortTransaction(Transaction transaction)
           
protected  void internalCompleteTransaction(Transaction transaction)
           
protected  void internalEnsureDoneReachable(ObjectContainer objectContainer)
          Internally ensures that the given object is doneReachable.
protected  int internalEnsureSurelyReachable(ObjectContainer objectContainer)
          Internally ensures that the given object is at least surelyReachable.
protected  void internalFinishTransaction(Transaction transaction)
           
protected  boolean isRunning()
          Returns wether this GarbageCollector is running.
protected  void notifyAboutTransactionActionAndRenewTransactionIfRequired()
           
 void notifyDatabaseObjectIsAboutToBeExported(ObjectID id)
          This method is called by DbInvokeClient to notify this garbageCollector that there is a reference which is to be exported to a client.
 void notifyDatabaseObjectIsExported(ObjectID id)
           
protected  void notifyEndOfWaitForCurrentTransactionsToCompletePhase()
          Informs this GarbageCollector, the the pre-phase is done, there are no transactions that are older than this garbage collector run.
 void notifyNamedObject(ObjectID id)
          This method is called by StoreManager to indicate that the object with the given id is a named object.
 void notifyNewObjectContainer(ObjectContainer objectContainer)
          This method is called by StoreManager in the event an object is freshly created.
 void notifyNewObjectName(ObjectContainer objectContainer)
          This method is called by StoreManager in the event an unnamed object receives a name.
protected  void processObjectContainerWhichWantsToBeSurelyReachable(Transaction transaction, ObjectContainer objectContainer)
          Processes an ObjectContainer which possibly has not already been registered as surely reachable.
protected  void processReferencesByThisObject(Transaction transaction, ObjectContainer objectContainer)
          Determines all references which are "contained" within the given object, adds them to the list of surelyReachableObjectsWhichHaveToBeMarkedAsSuch and then marks the given object as doneReachable.
protected  void processSurelyReachableObjectsWhichHaveToBeMarkedAsSuch()
          This is the "main()" method of the mark phase.
 void removeTransactionRequiredToComplete(Transaction ta)
           
protected  void renewTransactionIfRequired()
          Checks wether the current transaction has to be committed (because it accumulated too much changes) and if so, commits it and closes it.
 void run()
          The main method for garbage collection.
 void save()
          Save the internal state in the state to the server state properties.
protected  void setCurrentGarbageCollectionLevel(int to)
           
protected  void setPhase(int to)
           
 void shutdown()
           
 void start()
          Starts the garbage collection process.
protected  void startFilterDatabaseObjectReferencesAtExternalDatabaseGates()
           
 void startup()
          Start up and load the internal state from the server state properties.
protected  void sweepUnreachableObjects()
          Sweeps all objects which are considered unreachable.
protected  void waitForPhase(int newPhase)
           
 
Methods inherited from class org.ozoneDB.core.ServerComponent
clearChanged, hasChanged, setChanged
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

garbageCollectionThread

protected java.lang.Thread garbageCollectionThread
The thread which actually does the work.


transactionsRequiredToComplete

protected java.util.HashSet transactionsRequiredToComplete
The list of Transactions, which have to be completed before the GarbageCollector may start. It may not start earlier because the rollback of those transaction may make objects live which were believed to be dead.


currentGarbageCollectionLevel

protected int currentGarbageCollectionLevel
The number which represents the current garbage collection run. This number is the number mentioned in WizardObjectContainer.garbageCollectionLevel. There are tree sets of database objects


doneReachableGarbageCollectionLevel

protected int doneReachableGarbageCollectionLevel
The garbageCollectionLevel which objects have if they belong to the doneReachable set. This is currentGarbageCollectionLevel+1


phase

protected int phase
The current phase this GarbageCollector is in. Access only within synchronization to this object.


PHASE_IDLE

protected static final int PHASE_IDLE
Doing nothing

See Also:
Constant Field Values

PHASE_RUN_INITIATED

protected static final int PHASE_RUN_INITIATED
A run has been initiated

See Also:
Constant Field Values

PHASE_WAITING_FOR_OLD_TRANSACTIONS_TO_COMPLETE

protected static final int PHASE_WAITING_FOR_OLD_TRANSACTIONS_TO_COMPLETE
Waiting for the old transactions to complete

See Also:
Constant Field Values

PHASE_READY_TO_START

protected static final int PHASE_READY_TO_START
We may start, all old transactions are complete

See Also:
Constant Field Values

PHASE_MARKING

protected static final int PHASE_MARKING
We are started, we are marking all reachable objects

See Also:
Constant Field Values

PHASE_WAITING_FOR_NEW_TRANSACTIONS_TO_COMPLETE

protected static final int PHASE_WAITING_FOR_NEW_TRANSACTIONS_TO_COMPLETE
Waiting for the old transactions to complete

See Also:
Constant Field Values

PHASE_MARKING2

protected static final int PHASE_MARKING2
We are started, we are marking all reachable objects

See Also:
Constant Field Values

PHASE_SWEEPING

protected static final int PHASE_SWEEPING
We are started, we are sweeping all unreachable objects

See Also:
Constant Field Values

transaction

protected Transaction transaction
The current transaction of this garbageCollector.


actionsWithinTransactionCount

protected int actionsWithinTransactionCount
The count of actions which were done within this garbageCollector.


surelyReachableObjectsWhichHaveToBeMarkedAsSuch

protected org.ozoneDB.data.SimpleArrayList surelyReachableObjectsWhichHaveToBeMarkedAsSuch
A stack of ObjectIDs of objects, which are surely reachable, but still were not marked as such. Access only during synchronization on it.


surelyReachableObjectsWhichShouldHaveBeenProcessedButWereLockContented

protected java.util.LinkedList surelyReachableObjectsWhichShouldHaveBeenProcessedButWereLockContented
The list of ObjectIDs of objects which are surelyReachable and already were tried to be processed but where locking the objects failed. The processing the contents of this list should be retried some times later. To minimize overhead, access is only allowed during synchronization to surelyReachableObjectsWhichHaveToBeMarkedAsSuch


garbageCollectionLevelsLock

protected java.lang.Object garbageCollectionLevelsLock
This is the lock that everybody has to be synchronized to in order to be able to access any garbageCollectionLevel in any object within this database.


kill

protected boolean kill
Wether we should stop running as soon as possible. Synchronization is not required, because it does not matter wether we receive this signal soon or later.

Constructor Detail

GarbageCollector

protected GarbageCollector(Env env)
Creates a new garbage collector.

Method Detail

setCurrentGarbageCollectionLevel

protected void setCurrentGarbageCollectionLevel(int to)

startup

public void startup()
Description copied from class: ServerComponent
Start up and load the internal state from the server state properties.

Specified by:
startup in class ServerComponent

shutdown

public void shutdown()
Specified by:
shutdown in class ServerComponent

save

public void save()
Description copied from class: ServerComponent
Save the internal state in the state to the server state properties.

Specified by:
save in class ServerComponent

start

public void start()
Starts the garbage collection process.


addTransactionRequiredToComplete

public void addTransactionRequiredToComplete(java.lang.Object ta)

removeTransactionRequiredToComplete

public void removeTransactionRequiredToComplete(Transaction ta)

checkForEndOfWaitForCurrentTransactionsToCompletePhase

protected void checkForEndOfWaitForCurrentTransactionsToCompletePhase()

notifyEndOfWaitForCurrentTransactionsToCompletePhase

protected void notifyEndOfWaitForCurrentTransactionsToCompletePhase()
Informs this GarbageCollector, the the pre-phase is done, there are no transactions that are older than this garbage collector run.


incCurrentGarbageCollectorLevel

protected void incCurrentGarbageCollectorLevel()

run

public void run()
The main method for garbage collection.

Specified by:
run in interface java.lang.Runnable

notifyAboutTransactionActionAndRenewTransactionIfRequired

protected void notifyAboutTransactionActionAndRenewTransactionIfRequired()
                                                                  throws java.io.IOException,
                                                                         java.lang.ClassNotFoundException,
                                                                         org.ozoneDB.TransactionException
Throws:
java.io.IOException
java.lang.ClassNotFoundException
org.ozoneDB.TransactionException

renewTransactionIfRequired

protected void renewTransactionIfRequired()
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException,
                                          org.ozoneDB.TransactionException
Checks wether the current transaction has to be committed (because it accumulated too much changes) and if so, commits it and closes it. Afterwards (or if there has not been a current transaction already), it creates a new current transaction.

Throws:
java.io.IOException
java.lang.ClassNotFoundException
org.ozoneDB.TransactionException

completeTransaction

protected void completeTransaction()
                            throws java.io.IOException,
                                   java.lang.ClassNotFoundException
Completes the current transaction and releases it.

Throws:
java.io.IOException
java.lang.ClassNotFoundException

internalCompleteTransaction

protected void internalCompleteTransaction(Transaction transaction)
                                    throws java.io.IOException,
                                           java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

internalFinishTransaction

protected void internalFinishTransaction(Transaction transaction)
                                  throws java.io.IOException,
                                         java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

internalAbortTransaction

protected void internalAbortTransaction(Transaction transaction)
                                 throws java.io.IOException,
                                        java.lang.ClassNotFoundException
Throws:
java.io.IOException
java.lang.ClassNotFoundException

setPhase

protected void setPhase(int to)

waitForPhase

protected void waitForPhase(int newPhase)
                     throws java.lang.InterruptedException
Throws:
java.lang.InterruptedException

addRootSetElementsToSurelyReachableSet

protected void addRootSetElementsToSurelyReachableSet()

startFilterDatabaseObjectReferencesAtExternalDatabaseGates

protected void startFilterDatabaseObjectReferencesAtExternalDatabaseGates()

notifyDatabaseObjectIsExported

public void notifyDatabaseObjectIsExported(ObjectID id)

notifyDatabaseObjectIsAboutToBeExported

public void notifyDatabaseObjectIsAboutToBeExported(ObjectID id)
This method is called by DbInvokeClient to notify this garbageCollector that there is a reference which is to be exported to a client.


addAllNamedObjectsToSurelyReachableSet

protected void addAllNamedObjectsToSurelyReachableSet()
This method walks through all named objects and adds each of them to the surelyReachable set.


notifyNamedObject

public void notifyNamedObject(ObjectID id)
This method is called by StoreManager to indicate that the object with the given id is a named object.


notifyNewObjectName

public void notifyNewObjectName(ObjectContainer objectContainer)
This method is called by StoreManager in the event an unnamed object receives a name. This is the case if an object which was formerly unnamed gets a name.


notifyNewObjectContainer

public void notifyNewObjectContainer(ObjectContainer objectContainer)
This method is called by StoreManager in the event an object is freshly created.


ensureSurelyReachable

protected void ensureSurelyReachable(ObjectID id)
Calling this method makes sure that the object which is referenced by the given ID is at least surely reachable.


addObjectIDToSurelyReachableObjectsWhichHaveToBeMarkedAsSuch

protected void addObjectIDToSurelyReachableObjectsWhichHaveToBeMarkedAsSuch(ObjectID id)
This method may be called both from transaction threads and from GarbageCollector-Threads.


ensureSurelyReachable

protected void ensureSurelyReachable(ObjectContainer objectContainer)
Calling this method makes sure that the object which is referenced by the given ID is at least surely reachable. This method is called by non-GarbageCollector-Threads.


isRunning

protected boolean isRunning()
Returns wether this GarbageCollector is running. This method must be called during synchronization on this GarbageCollector.


processSurelyReachableObjectsWhichHaveToBeMarkedAsSuch

protected void processSurelyReachableObjectsWhichHaveToBeMarkedAsSuch()
This is the "main()" method of the mark phase.


internalEnsureSurelyReachable

protected int internalEnsureSurelyReachable(ObjectContainer objectContainer)
Internally ensures that the given object is at least surelyReachable.

Returns:
<0 if this object has been updated. If this is the case, this object has to join a transaction which will complete successfully. =0 if this object has not been updated, it is surelyReachable >0 if this object has not been updated, it is processedReachable

ensureDoneReachable

protected void ensureDoneReachable(ObjectContainer objectContainer)
Ensures that the given object is doneReachable.


internalEnsureDoneReachable

protected void internalEnsureDoneReachable(ObjectContainer objectContainer)
Internally ensures that the given object is doneReachable.


processObjectContainerWhichWantsToBeSurelyReachable

protected void processObjectContainerWhichWantsToBeSurelyReachable(Transaction transaction,
                                                                   ObjectContainer objectContainer)
Processes an ObjectContainer which possibly has not already been registered as surely reachable. This method may be called only by the garbage collector thread.

Parameters:
transaction -
objectContainer -

processReferencesByThisObject

protected void processReferencesByThisObject(Transaction transaction,
                                             ObjectContainer objectContainer)
Determines all references which are "contained" within the given object, adds them to the list of surelyReachableObjectsWhichHaveToBeMarkedAsSuch and then marks the given object as doneReachable. This method may be called by any CommandThread.


deferProcessingOfObjectContainerDueToLockContention

protected void deferProcessingOfObjectContainerDueToLockContention(ObjectContainer objectContainer)
Defers the processing of the given objectContainer because locking was not possible at current time. Because objectContainers may be modified in the meantime (in the case they are unloaded and loaded again, we would hold an old copy of objectContainer which does not reflect the changes made to the new objectContainer), it's not wise to queue the ObjectContainer. Instead, we have to queue the ObjectID of that ObjectContainer.


interceptInvocationPre

public void interceptInvocationPre(Transaction transaction,
                                   ObjectContainer callee,
                                   java.lang.Object[] args)
This method is called by Transactions to indicate that a method of the object callee will be called by the transaction. This method will always be called, even if the GarbageCollector is not startet.


interceptInvocationPost

public void interceptInvocationPost(Transaction transaction,
                                    ObjectContainer callee,
                                    java.lang.Object result)
This method is called by Transactions to indicate that a method of the object callee was called by the transaction. This method will always be called, even if the GarbageCollector is not startet.


checkForProxyBorderCross

protected void checkForProxyBorderCross(java.lang.Object o)
This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys. If so, the database objects referenced by these proxies are considered surelyReachable.


checkForProxyBorderCross

protected void checkForProxyBorderCross(org.ozoneDB.OzoneProxy o)
This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys. If so, the database objects referenced by these proxies are considered surelyReachable.


checkForProxyBorderCross

protected void checkForProxyBorderCross(org.ozoneDB.OzoneCompatible o)
This method checks wether the given object or the object graph reachable from this given object contains OzoneProxys. If so, the database objects referenced by these proxies are considered surelyReachable.


sweepUnreachableObjects

protected void sweepUnreachableObjects()
Sweeps all objects which are considered unreachable.


ozone core API

Copyright (C) The Ozone Database Project - www.ozone-db.org. All rights reserved.