1 4 package com.tc.objectserver.persistence.sleepycat; 5 6 import com.sleepycat.je.DatabaseException; 7 import com.tc.logging.TCLogging; 8 import com.tc.object.ObjectID; 9 import com.tc.objectserver.core.api.ManagedObject; 10 import com.tc.objectserver.core.api.ManagedObjectState; 11 import com.tc.objectserver.managedobject.ManagedObjectChangeListener; 12 import com.tc.objectserver.managedobject.ManagedObjectChangeListenerProviderImpl; 13 import com.tc.objectserver.managedobject.ManagedObjectStateFactory; 14 import com.tc.objectserver.persistence.api.ClassPersistor; 15 import com.tc.objectserver.persistence.api.ClientStatePersistor; 16 import com.tc.objectserver.persistence.api.ManagedObjectPersistor; 17 import com.tc.objectserver.persistence.api.TransactionPersistor; 18 import com.tc.objectserver.persistence.impl.StringIndexImpl; 19 import com.tc.util.Assert; 20 import com.tc.util.SyncObjectIdSet; 21 22 import gnu.trove.TObjectLongHashMap; 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.Collection ; 27 import java.util.HashSet ; 28 import java.util.Iterator ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 public class SleepycatTCDBDiff { 33 34 private static final String VERSION_STRING = "SleepycatTCDBDiff [ Ver 0.2]"; 35 36 private final SleepycatPersistor sdb1; 37 private final SleepycatPersistor sdb2; 38 private final File d1; 39 private final File d2; 40 private final boolean moDiff; 41 private final ManagedObjectStateFactory mosf1; 42 private final ManagedObjectStateFactory mosf2; 43 44 public SleepycatTCDBDiff(File d1, File d2, boolean moDiff) throws DatabaseException, IOException { 45 this.d1 = d1; 46 this.d2 = d2; 47 this.moDiff = moDiff; 48 this.sdb1 = new SleepycatPersistor(TCLogging.getLogger(SleepycatTCDBDiff.class), new DBEnvironment(true, d1), 49 new CustomSerializationAdapterFactory()); 50 this.sdb2 = new SleepycatPersistor(TCLogging.getLogger(SleepycatTCDBDiff.class), new DBEnvironment(true, d2), 51 new CustomSerializationAdapterFactory()); 52 53 ManagedObjectChangeListenerProviderImpl moclp = new ManagedObjectChangeListenerProviderImpl(); 55 moclp.setListener(new ManagedObjectChangeListener() { 56 57 public void changed(ObjectID changedObject, ObjectID oldReference, ObjectID newReference) { 58 } 60 61 }); 62 ManagedObjectStateFactory.disableSingleton(true); 63 mosf1 = ManagedObjectStateFactory.createInstance(moclp, sdb1); 64 mosf2 = ManagedObjectStateFactory.createInstance(moclp, sdb2); 65 66 } 67 68 public void diff() { 69 log("Diffing [1] " + d1 + " and [2] " + d2); 70 diffStringIndexer((StringIndexImpl) sdb1.getStringIndex(), (StringIndexImpl) sdb2.getStringIndex()); 71 diffManagedObjects(sdb1.getManagedObjectPersistor(), sdb2.getManagedObjectPersistor()); 72 diffClientStates(sdb1.getClientStatePersistor(), sdb2.getClientStatePersistor()); 73 diffTransactions(sdb1.getTransactionPersistor(), sdb2.getTransactionPersistor()); 74 diffGeneratedClasses(sdb1.getClassPersistor(), sdb2.getClassPersistor()); 75 } 76 77 private void diffGeneratedClasses(ClassPersistor cp1, ClassPersistor cp2) { 78 log("Diffing Generated Classes...(Partial implementation)"); 79 Map m1 = cp1.retrieveAllClasses(); 80 Map m2 = cp2.retrieveAllClasses(); 81 if (!m1.keySet().equals(m2.keySet())) { 82 log("*** [1] containing " + m1.size() + " generated classes differs from [2] containing " + m2.size()); 83 } 84 } 85 86 private void diffTransactions(TransactionPersistor tp1, TransactionPersistor tp2) { 87 log("Diffing Transactions..."); 88 Collection txns1 = tp1.loadAllGlobalTransactionDescriptors(); 89 Collection txns2 = tp2.loadAllGlobalTransactionDescriptors(); 90 if (!txns1.equals(txns2)) { 91 log("*** [1] containing " + txns1.size() + " Transactions differs from [2] containing " + txns2.size() 92 + " Transactions"); 93 } 94 } 95 96 private void diffClientStates(ClientStatePersistor cp1, ClientStatePersistor cp2) { 97 log("Diffing ClientStates..."); 98 Set cids1 = cp1.loadClientIDs(); 99 Set cids2 = cp2.loadClientIDs(); 100 if (!cids1.equals(cids2)) { 101 log("*** [1] containing " + cids1.size() + " ClientIDs differs from [2] containing " + cids2.size() 102 + " ClientIDs"); 103 } 104 } 105 106 private void diffManagedObjects(ManagedObjectPersistor mp1, ManagedObjectPersistor mp2) { 107 log("Diffing ManagedObjects...(Partial implementation)"); 108 SyncObjectIdSet oids1 = mp1.getAllObjectIDs(); 109 SyncObjectIdSet oids2 = mp2.getAllObjectIDs(); 110 if (!oids1.equals(oids2)) { 111 log("*** [1] containing " + oids1.size() + " ObjectIDs differs from [2] containing " + oids2.size() 112 + " ObjectIDs"); 113 } 114 115 if (!moDiff) return; 116 117 Set common = new HashSet (oids1); 120 common.retainAll(oids2); 121 for (Iterator i = common.iterator(); i.hasNext();) { 122 ObjectID oid = (ObjectID) i.next(); 123 ManagedObjectStateFactory.setInstance(mosf1); 124 ManagedObject mo1 = mp1.loadObjectByID(oid); 125 ManagedObjectStateFactory.setInstance(mosf2); 126 ManagedObject mo2 = mp2.loadObjectByID(oid); 127 Assert.assertEquals(mo1.getID(), mo2.getID()); 128 ManagedObjectState ms1 = mo1.getManagedObjectState(); 129 ManagedObjectState ms2 = mo2.getManagedObjectState(); 130 if (!ms1.equals(ms2)) { 131 log("****** [1] " + ms1 + " differs from [2] " + ms2); 132 } 133 } 134 135 oids1.removeAll(common); 137 if (!oids1.isEmpty()) { 138 log("****** [1] contains " + oids1 + " not in [2]"); 139 } 140 oids2.removeAll(common); 141 if (!oids2.isEmpty()) { 142 log("****** [2] contains " + oids2 + " not in [1]"); 143 } 144 } 145 146 private static void log(String message) { 147 System.out.println(message); 148 } 149 150 private void diffStringIndexer(StringIndexImpl stringIndex1, StringIndexImpl stringIndex2) { 151 log("Diffing StringIndexes..."); 152 TObjectLongHashMap map1 = stringIndex1.getString2LongMappings(); 153 TObjectLongHashMap map2 = stringIndex2.getString2LongMappings(); 154 if (!map1.equals(map2)) { 155 log("*** [1] containing " + map1.size() + " mapping differs from [2] containing " + map2.size() + " mapping"); 156 } 157 } 158 159 public static void main(String [] args) { 160 boolean moDiff = false; 161 162 log(VERSION_STRING); 163 if (args == null || args.length < 2) { 164 usage(); 165 System.exit(-1); 166 } else if (args.length > 2) { 167 Assert.assertEquals("-mo", args[2]); 168 moDiff = true; 169 } 170 try { 171 File d1 = new File (args[0]); 172 validateDir(d1); 173 File d2 = new File (args[1]); 174 validateDir(d2); 175 176 SleepycatTCDBDiff sdiff = new SleepycatTCDBDiff(d1, d2, moDiff); 177 sdiff.diff(); 178 } catch (Exception ex) { 179 ex.printStackTrace(); 180 System.exit(-2); 181 } 182 } 183 184 private static void validateDir(File dir) { 185 if (!dir.exists() || !dir.isDirectory()) { throw new RuntimeException ("Not a valid directory : " + dir); } 186 } 187 188 private static void usage() { 189 log("Usage: SleepycatTCDBDiff <environment home directory1> <environment home directory2> [-mo]"); 190 } 191 192 } 193 | Popular Tags |