1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 package org.coach.tracing.server; 26 27 import org.coach.idltree.*; 28 import org.coach.tracing.*; 29 import org.coach.tracing.api.*; 30 import java.io.*; 31 import java.util.*; 32 33 public class EventDataBase 34 { 35 private static int CashSize = 1001; 36 private EventRecord[] eventCash = new EventRecord[CashSize]; 37 38 private RandomAccessFile valueFile = null; 39 private RandomAccessFile eventFile = null; 40 private Hashtable nameIndexTable = new Hashtable(); 41 private Hashtable indexNameTable = new Hashtable(); 42 private HashMap identityTreeTable = new HashMap(); 43 private HashMap identityIndexTable = new HashMap(); 44 private Identity rootIdentity = new Identity(IdentityKind.CCM_NODE, "", "", (long)-1); 45 private static Long rootKey = new Long (-1); 46 private static HashMap trailMap = new HashMap(); 47 private static HashMap unmatched = new HashMap(); 48 private static EventSorter eventSorter = null; 49 private static EventDataBase eventDB = null; 50 51 private File dbDir = null; 52 private long eventCount = 0; 53 private int identityCount = 0; 54 private long messageCount = 0; 55 private int nameIndex = 0; 56 private int unmatchCount = 0; 57 58 public static EventDataBase getEventDB() 59 { 60 if (eventDB == null) 61 { 62 eventDB = new EventDataBase(); 63 eventSorter = new EventSorter(eventDB); 64 } 65 return eventDB; 66 } 67 68 private EventDataBase() 69 { 70 if (eventFile == null) 71 { 72 try 73 { 74 identityTreeTable.put(rootIdentity, new HashMap()); 75 identityIndexTable.put(rootKey, rootIdentity); 76 77 dbDir = new File(System.getProperty("coach.tracing.db.dir", ".")); 78 dbDir.mkdirs(); 79 80 File f = new File(dbDir, "value.db"); 81 if (f.exists()) 82 { 83 f.delete(); 84 } 85 valueFile = new RandomAccessFile(f.getPath(), "rw"); 86 87 f = new File(dbDir, "event.db"); 88 if (f.exists()) 89 { 90 f.delete(); 91 } 92 eventFile = new RandomAccessFile(f.getPath(), "rw"); 93 94 f = new File(dbDir, "identity.db"); 95 if (f.exists()) 96 { 97 f.delete(); 98 } 99 } 100 catch (Exception e) 101 { 102 e.printStackTrace(); 103 } 104 } 105 } 106 107 112 public synchronized void save(String name) 113 { 114 try 115 { 116 File dbDir = new File(System.getProperty("coach.event.db", ".")); 117 File saveDir = new File(dbDir, name); 118 eventFile.close(); 119 valueFile.close(); 120 saveDir.mkdirs(); 121 122 saveIdentities(new File(saveDir, "identity.db")); 123 124 File f1 = new File(saveDir, "names.db"); 125 PrintWriter nameFile = new PrintWriter(new FileWriter(f1.getPath())); 126 for (int i = 0; i < nameIndex; i++) 127 { 128 nameFile.println(readName(i)); 129 } 130 nameFile.close(); 131 132 f1 = new File(dbDir, "value.db"); 133 File f2 = new File(saveDir, "value.db"); 134 f1.renameTo(f2); 135 valueFile = new RandomAccessFile(f1.getPath(), "rw"); 136 137 f1 = new File(dbDir, "event.db"); 138 f2 = new File(saveDir, "event.db"); 139 f1.renameTo(f2); 140 eventFile = new RandomAccessFile(f1.getPath(), "rw"); 141 } 142 catch (Exception e) 143 { 144 e.printStackTrace(); 145 throw new RuntimeException ("Failed to save events"); 146 } 147 } 148 149 public synchronized void load(String name) 150 { 151 try 152 { 153 File dbDir = new File(System.getProperty("coach.event.db", ".")); 154 File saveDir = new File(dbDir, name); 155 File f = new File(saveDir, "names.db"); 156 157 BufferedReader nameFile = new BufferedReader(new FileReader(f.getPath())); 158 nameIndexTable.clear(); 159 indexNameTable.clear(); 160 identityTreeTable.clear(); 161 identityIndexTable.clear(); 162 identityCount = 0; 163 164 nameIndex = 0; 165 String line = nameFile.readLine(); 166 while (line != null) 167 { 168 writeName(line); 169 line = nameFile.readLine(); 170 } 171 nameFile.close(); 172 173 f = new File(saveDir, "value.db"); 174 valueFile = new RandomAccessFile(f.getPath(), "rw"); 175 f = new File(saveDir, "event.db"); 176 eventFile = new RandomAccessFile(f.getPath(), "rw"); 177 178 180 f = new File(saveDir, "identity.db"); 181 loadIdentities(f); 182 183 } 184 catch (Exception e) 185 { 186 e.printStackTrace(); 187 throw new RuntimeException ("Failed to load events"); 188 } 189 } 190 191 public synchronized void clear() 192 { 193 try 194 { 195 if (eventFile != null) 197 { 198 eventFile.close(); 199 valueFile.close(); 200 } 201 202 File f = new File(dbDir, "value.db"); 203 if (f.exists()) 204 { 205 f.delete(); 206 } 207 valueFile = new RandomAccessFile(f.getPath(), "rw"); 208 209 f = new File(dbDir, "event.db"); 210 if (f.exists()) 211 { 212 f.delete(); 213 } 214 eventFile = new RandomAccessFile(f.getPath(), "rw"); 215 216 f = new File(dbDir, "identity.db"); 217 if (f.exists()) 218 { 219 f.delete(); 220 } 221 222 f = new File(dbDir, "key.db"); 223 if (f.exists()) 224 { 225 f.delete(); 226 } 227 nameIndexTable.clear(); 228 indexNameTable.clear(); 229 nameIndex = 0; 230 eventCount = 0; 231 identityCount = 0; 232 } 233 catch (Exception e) 234 { 235 e.printStackTrace(); 236 } 237 } 238 239 public synchronized void addEvents(TraceEvent[] events) 240 { 241 for (int i = 0; i < events.length; i++) 242 { 243 EventRecord r = new EventRecord(events[i]); 244 writeEvent(r); 245 } 246 } 247 248 public synchronized EventRecord getEventAt(long index) 249 { 250 return eventSorter.getEventAt(index); 251 } 252 253 public synchronized long getEventIndex(long key) 254 { 255 return eventSorter.getEventIndex(key); 256 } 257 258 public synchronized EventRecord[] getEvents(long start, long end) 259 { 260 return eventSorter.getEvents(start, end); 261 } 262 263 public void link(EventRecord r1, EventRecord r2) 264 { 265 r2.tr.linkKey = r1.getKey(); 266 r1.tr.linkKey = r2.getKey(); 267 updateEvent(r1); 268 updateEvent(r2); 269 messageCount++; 270 } 271 272 private String readName(int index) 273 { 274 return (String )indexNameTable.get(new Integer (index)); 275 } 276 277 private int writeName(String name) 278 { 279 if (name == null) 280 { 281 throw new RuntimeException ("name should not be null"); 282 } 283 Integer index = (Integer )nameIndexTable.get(name); 284 if (index == null) 285 { 286 index = new Integer (nameIndex++); 287 nameIndexTable.put(name, index); 288 indexNameTable.put(index, name); 289 } 290 return index.intValue(); 291 } 292 293 private long writeValue(IdlNode idlNode) 294 { 295 if (idlNode == null) 296 { 297 return -1; 299 } 300 try 301 { 302 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 303 ObjectOutputStream out = new ObjectOutputStream(bout); 304 out.writeObject(idlNode); 305 out.close(); 306 byte[] buf = bout.toByteArray(); 307 long index = valueFile.length(); 308 valueFile.seek(index); 309 valueFile.writeInt(buf.length); 310 valueFile.write(buf); 311 return index; 312 } 313 catch (Exception ex) 314 { 315 ex.printStackTrace(); 316 } 317 throw new RuntimeException ("Failed to write event"); 318 } 319 320 public String getParameterValues(long key) { 321 IdlNode idlNode = readValue(key); 322 if (idlNode == null) { 323 return ""; 325 } 326 XmlWriter w = new XmlWriter(); 327 XmlNode.write(idlNode, w); 328 return w.toString(); 329 } 330 331 public IdlNode readValue(long index) 332 { 333 if (index == -1) 334 { 335 return null; 337 } 338 try 339 { 340 valueFile.seek(index); 341 int size = valueFile.readInt(); 342 byte[] buf = new byte[size]; 343 valueFile.read(buf); 344 ByteArrayInputStream bin = new ByteArrayInputStream(buf); 345 ObjectInputStream in = new ObjectInputStream(bin); 346 IdlNode n = (IdlNode)in.readObject(); 347 in.close(); 348 return n; 349 } 350 catch (Exception e) 351 { 352 e.printStackTrace(); 353 } 354 throw new RuntimeException ("Failed to read value"); 355 } 356 357 public long getEventCount() 358 { 359 return eventCount; 360 } 361 362 public int getIdentityCount() 363 { 364 return identityCount; 365 } 366 367 public long getMessageCount() 368 { 369 return messageCount; 370 } 371 372 public long getUnmatchedEventCount() 373 { 374 return eventCount - (messageCount * 2); 375 } 376 377 public synchronized void writeEvent(EventRecord e) 378 { 379 try 380 { 381 e.tr.key_id = eventFile.length(); 382 writeEvent(e, e.tr.key_id); 383 eventCash[(int)(e.tr.key_id % CashSize)] = e; 384 eventCount++; 385 386 match(e); 387 388 eventSorter.add(e); 389 return; 390 } 391 catch (Exception ex) 392 { 393 ex.printStackTrace(); 394 } 395 throw new RuntimeException ("Failed to write event"); 396 } 397 398 public synchronized void updateEvent(EventRecord e) 399 { 400 writeEvent(e, e.tr.key_id); 401 eventCash[(int)(e.tr.key_id % CashSize)] = e; 402 } 403 404 private void writeEvent(EventRecord e, long index) 405 { 406 try 407 { 408 eventFile.seek(e.tr.key_id); 409 eventFile.writeLong(e.tr.key_id); 411 if (e.identity != null) 412 { 413 e.tr.identityKey = writeIdentity(e.identity); 414 } 415 eventFile.writeLong(e.tr.identityKey); 416 eventFile.writeInt(writeName(e.tr.trail_label)); 417 eventFile.writeInt(writeName(e.tr.message_id)); 418 eventFile.writeInt(writeName(e.tr.trail_id)); 419 eventFile.writeInt(e.tr.event_counter); 420 eventFile.writeInt(writeName(e.tr.op_name)); 421 eventFile.writeInt(e.tr.interaction_point.value()); 422 e.tr.parametersKey = writeValue(e.idlNode); 423 eventFile.writeLong(e.tr.parametersKey); 424 eventFile.writeLong(e.tr.linkKey); 425 eventFile.writeInt(writeName(e.tr.thread_id)); 426 eventFile.writeLong(e.tr.time_stamp); 427 return; 428 } 429 catch (Exception ex) 430 { 431 ex.printStackTrace(); 432 } 433 throw new RuntimeException ("Failed to write event"); 434 } 435 436 public synchronized EventRecord readEvent(long key) 437 { 438 EventRecord e = eventCash[(int)(key % CashSize)]; 439 if (e != null && e.getKey() == key) 440 { 441 return e; 442 } 443 444 try 445 { 446 eventFile.seek(key); 447 448 e = new EventRecord(); 449 e.tr.key_id = eventFile.readLong(); 451 if (e.tr.key_id != key) 452 { 453 throw new RuntimeException ("Key does not match database record key value!"); 454 } 455 e.tr.identityKey = eventFile.readLong(); 456 e.tr.trail_label = readName(eventFile.readInt()); 457 e.tr.message_id = readName(eventFile.readInt()); 458 e.tr.trail_id = readName(eventFile.readInt()); 459 e.tr.event_counter = eventFile.readInt(); 460 e.tr.op_name = readName(eventFile.readInt()); 461 e.tr.interaction_point = InteractionPoint.from_int(eventFile.readInt()); 462 e.tr.parametersKey = eventFile.readLong(); 463 e.tr.linkKey = eventFile.readLong(); 464 e.tr.thread_id = readName(eventFile.readInt()); 465 e.tr.time_stamp = eventFile.readLong(); 466 467 eventCash[(int)(e.tr.key_id % CashSize)] = e; 468 469 return e; 470 } 471 catch (Exception ex) 472 { 473 ex.printStackTrace(); 474 } 475 throw new RuntimeException ("Failed to read event"); 476 } 477 478 private long writeIdentity(IdentityDescriptor d) 479 { 480 Long parent = rootKey; 481 Identity id = null; 482 483 Long key = getIdentityKey(parent, d.node_name); 484 485 if (key == null) 486 { 487 id = new Identity(); 488 id.kind = IdentityKind.CCM_NODE; 489 id.name = d.node_name; 490 id.type = d.node_ip; 491 id.linkKey = parent.longValue(); 492 key = putIdentity(parent, id); 493 } 494 parent = key; 495 496 key = getIdentityKey(parent, d.process_id); 497 498 if (key == null) 499 { 500 id = new Identity(); 501 id.kind = IdentityKind.CCM_PROCESS; 502 id.name = d.process_id; 503 id.type = ""; 504 id.linkKey = parent.longValue(); 505 key = putIdentity(parent, id); 506 } 507 508 parent = key; 509 510 if (!d.cnt_name.equals("")) 511 { 512 key = getIdentityKey(parent, d.cnt_name); 513 514 if (key == null) 515 { 516 id = new Identity(); 517 id.kind = IdentityKind.CCM_CONTAINER; 518 id.name = d.cnt_name; 519 id.type = d.cnt_type; 520 id.linkKey = parent.longValue(); 521 key = putIdentity(parent, id); 522 } 523 524 parent = key; 525 } 526 527 if (!d.cmp_name.equals("")) 528 { 529 key = getIdentityKey(parent, d.cmp_name); 530 531 if (key == null) 532 { 533 id = new Identity(); 534 id.kind = IdentityKind.CCM_COMPONENT; 535 id.name = d.cmp_name; 536 id.type = d.cmp_type; 537 id.linkKey = parent.longValue(); 538 key = putIdentity(parent, id); 539 } 540 541 parent = key; 542 } 543 544 key = getIdentityKey(parent, d.object_instance_id); 545 546 if (key == null) 547 { 548 id = new Identity(); 549 id.kind = IdentityKind.CCM_OBJECT; 550 id.name = d.object_instance_id; 551 id.type = d.object_repository_id; 552 id.linkKey = parent.longValue(); 553 key = putIdentity(parent, id); 554 } 555 556 return key.longValue(); 557 } 558 559 private Long getIdentityKey(Long parent, String name) 560 { 561 Identity id = (Identity)identityIndexTable.get(parent); 562 563 if (id == null) 564 throw new RuntimeException ("unexpected null entry in identityIndexTable for: " + parent); 565 566 HashMap t = (HashMap)identityTreeTable.get(id); 567 568 if (t == null) 569 return null; 570 571 return (Long )t.get(name); 572 } 573 574 private Long putIdentity(Long parent, Identity id) 575 { 576 Identity parentId = (Identity)identityIndexTable.get(parent); 577 578 if (parentId == null) 579 throw new RuntimeException ("unexpected null entry in identityIndexTable for: " + parent); 580 581 583 HashMap t = (HashMap)identityTreeTable.get(parentId); 584 585 if (t == null) 586 { 587 589 t = new HashMap(); 590 591 identityTreeTable.put(parentId, t); 592 } 593 594 596 Long key = (Long )t.get(id.name); 597 598 if (key == null) 599 { 600 602 key = new Long (identityCount++); 603 604 identityIndexTable.put(key, id); 605 606 t.put(id.name, key); 607 } 608 609 return key; 610 } 611 612 private void saveIdentities(File f) 613 { 614 try 615 { 616 DataOutputStream identityFile = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(f))); 617 618 for (long i = 0; i < identityCount; i++) 619 { 620 Identity id = (Identity)identityIndexTable.get(new Long (i)); 621 identityFile.writeInt(id.kind.value()); 622 identityFile.writeInt(writeName(id.name)); 623 identityFile.writeInt(writeName(id.type)); 624 identityFile.writeLong(id.linkKey); 625 } 626 627 identityFile.close(); 628 } 629 catch (Exception _ex) 630 { 631 _ex.printStackTrace(); 632 633 throw new RuntimeException ("Failed to write identity"); 634 } 635 } 636 637 private void loadIdentities(File f) 638 { 639 try 640 { 641 DataInputStream identityFile = new DataInputStream(new BufferedInputStream(new FileInputStream(f))); 642 identityCount = 0; 643 644 while(identityFile.available() > 0) 645 { 646 Identity id = new Identity(); 647 id.kind = IdentityKind.from_int(identityFile.readInt()); 648 id.name = readName(identityFile.readInt()); 649 id.type = readName(identityFile.readInt()); 650 id.linkKey = identityFile.readLong(); 651 identityIndexTable.put(new Long (identityCount++), id); 652 } 653 identityFile.close(); 654 } 655 catch (Exception _ex) 656 { 657 _ex.printStackTrace(); 658 throw new RuntimeException ("Failed to read identity"); 659 } 660 } 661 662 public Identity getIdentity(Long key) 663 { 664 return (Identity)identityIndexTable.get(key); 665 } 666 667 private void match(EventRecord r) 668 { 669 try 670 { 671 String messageId = r.getMessageId(); 672 674 EventRecord r2 = (EventRecord)unmatched.get(messageId); 675 if (r2 != null) 676 { 677 link(r, r2); 678 unmatched.remove(messageId); 679 unmatchCount--; 680 } 681 else 682 { 683 unmatched.put(messageId, r); 684 unmatchCount++; 685 } 686 } 687 catch(Exception e) 688 { 689 e.printStackTrace(); 690 } 691 } 692 } 693 | Popular Tags |