1 16 package com.sun.slamd.example; 17 18 19 20 import java.io.*; 21 import java.security.*; 22 import java.util.*; 23 import netscape.ldap.*; 24 import netscape.ldap.factory.*; 25 import com.sun.slamd.job.*; 26 import com.sun.slamd.parameter.*; 27 import com.sun.slamd.stat.*; 28 29 30 31 46 public class LDAPPrimeJobClass 47 extends JobClass 48 { 49 53 public static final String STAT_TRACKER_SEARCH_TIME = "Search Time (ms)"; 54 55 56 57 61 public static final String STAT_TRACKER_ENTRY_COUNT = "Entries Returned"; 62 63 64 65 69 public static final String STAT_TRACKER_SEARCHES_COMPLETED = 70 "Searches Completed"; 71 72 73 74 78 public static final String STAT_TRACKER_TOTAL_SEARCHES_COMPLETED = 79 "Total Searches Completed"; 80 81 82 83 87 public static final String STAT_TRACKER_EXCEPTIONS_CAUGHT = 88 "Exceptions Caught"; 89 90 91 92 IntegerParameter clientsParameter = 94 new IntegerParameter("num_clients", "Number of Clients", 95 "The number of clients over which the priming " + 96 "should be distributed", true, 0, true, 1, false, 97 0); 98 99 IntegerParameter portParameter = 101 new IntegerParameter("ldapport", "Directory Server Port", 102 "The port number for the LDAP directory server", 103 true, 389, true, 1, true, 65535); 104 105 IntegerParameter rangeStartParameter = 107 new IntegerParameter("rangestart", "Value Range Start", 108 "The value that should be used as the start of " + 109 "the range when priming the job.", true, 0, false, 110 0, false, 0); 111 112 IntegerParameter rangeStopParameter = 114 new IntegerParameter("rangestop", "Value Range End", 115 "The value that should be used as the end of " + 116 "the range when priming the job.", true, 0, false, 117 0, false, 0); 118 119 IntegerParameter timeLimitParameter = 122 new IntegerParameter("timelimit", "Search Time Limit", 123 "The maximum length of time to wait for the " + 124 "result of a search operation (0 to wait forever)", 125 true, 0, true, 0, false, 0); 126 127 PlaceholderParameter placeholder = new PlaceholderParameter(); 129 130 StringParameter searchBaseParameter = 132 new StringParameter("searchbase", "Search Base", 133 "The DN of the entry to use as the search base", 134 false, ""); 135 136 StringParameter attributeNameParameter = 138 new StringParameter("attrname", "Attribute Name", 139 "The name of the LDAP attribute that contains the " + 140 "sequentially-incrementing value used to prime " + 141 "the directory", true, ""); 142 143 StringParameter bindDNParameter = 145 new StringParameter("binddn", "Bind DN", 146 "The DN to use to bind to the server", false, ""); 147 148 StringParameter hostParameter = 150 new StringParameter("ldaphost", "Directory Server Host", 151 "The DNS hostname or IP address of the LDAP " + 152 "directory server", true, ""); 153 154 PasswordParameter bindPWParameter = 156 new PasswordParameter("bindpw", "Bind Password", 157 "The password for the bind DN", false, ""); 158 159 160 static int ldapPort; 162 static int numClients; 163 static int rangeStart; 164 static int rangeStop; 165 static int timeLimit; 166 static String attributeName; 167 static String searchBase; 168 static String bindDN; 169 static String bindPassword; 170 static String ldapHost; 171 172 173 static int clientMax; 175 static int clientMin; 176 177 178 int threadMax; 180 int threadMin; 181 182 183 LDAPConnection conn; 185 186 187 AccumulatingTracker totalSearches; 189 IncrementalTracker exceptionsCaught; 190 IncrementalTracker successfulSearches; 191 IntegerValueTracker entryCount; 192 TimeTracker searchTime; 193 194 195 196 197 203 public LDAPPrimeJobClass() 204 { 205 super(); 206 } 207 208 209 210 215 public String getJobName() 216 { 217 return "LDAP Prime Job"; 218 } 219 220 221 222 227 public String getJobDescription() 228 { 229 return "This job can be used to prime the caches of an LDAP directory " + 230 "server that contains a contrived data set with an attribute " + 231 "whose value is a sequentially incrementing number"; 232 } 233 234 235 236 242 public String getJobCategoryName() 243 { 244 return "LDAP"; 245 } 246 247 248 249 256 public ParameterList getParameterStubs() 257 { 258 Parameter[] parameters = new Parameter[] 259 { 260 placeholder, 261 hostParameter, 262 portParameter, 263 bindDNParameter, 264 bindPWParameter, 265 placeholder, 266 searchBaseParameter, 267 attributeNameParameter, 268 rangeStartParameter, 269 rangeStopParameter, 270 timeLimitParameter 271 }; 272 273 return new ParameterList(parameters); 274 } 275 276 277 278 288 public ParameterList getClientSideParameterStubs() 289 { 290 Parameter[] parameters = new Parameter[] 291 { 292 placeholder, 293 hostParameter, 294 portParameter, 295 bindDNParameter, 296 bindPWParameter, 297 placeholder, 298 searchBaseParameter, 299 attributeNameParameter, 300 rangeStartParameter, 301 rangeStopParameter, 302 timeLimitParameter, 303 clientsParameter 304 }; 305 306 return new ParameterList(parameters); 307 } 308 309 310 311 333 public StatTracker[] getStatTrackerStubs(String clientID, String threadID, 334 int collectionInterval) 335 { 336 return new StatTracker[] 337 { 338 new IncrementalTracker(clientID, threadID, 339 STAT_TRACKER_SEARCHES_COMPLETED, 340 collectionInterval), 341 new IncrementalTracker(clientID, threadID, STAT_TRACKER_EXCEPTIONS_CAUGHT, 342 collectionInterval), 343 new IntegerValueTracker(clientID, threadID, STAT_TRACKER_ENTRY_COUNT, 344 collectionInterval), 345 new TimeTracker(clientID, threadID, STAT_TRACKER_SEARCH_TIME, 346 collectionInterval) 347 }; 348 } 349 350 351 352 357 public StatTracker[] getStatTrackers() 358 { 359 return new StatTracker[] 360 { 361 successfulSearches, 362 totalSearches, 363 exceptionsCaught, 364 entryCount, 365 searchTime 366 }; 367 } 368 369 370 371 393 public void validateJobInfo(int numClients, int threadsPerClient, 394 int threadStartupDelay, Date startTime, 395 Date stopTime, int duration, 396 int collectionInterval, ParameterList parameters) 397 throws InvalidValueException 398 { 399 IntegerParameter clientsParameter = 404 new IntegerParameter("num_clients", numClients); 405 parameters.addParameter(clientsParameter); 406 } 407 408 409 410 418 public boolean providesParameterTest() 419 { 420 return true; 421 } 422 423 424 425 452 public boolean testJobParameters(ParameterList parameters, 453 ArrayList outputMessages) 454 { 455 StringParameter hostParam = 457 parameters.getStringParameter(hostParameter.getName()); 458 if ((hostParam == null) || (! hostParam.hasValue())) 459 { 460 outputMessages.add("ERROR: No directory server address was provided."); 461 return false; 462 } 463 String host = hostParam.getStringValue(); 464 465 466 IntegerParameter portParam = 467 parameters.getIntegerParameter(portParameter.getName()); 468 if ((portParam == null) || (! hostParam.hasValue())) 469 { 470 outputMessages.add("ERROR: No directory server port was provided."); 471 return false; 472 } 473 int port = portParam.getIntValue(); 474 475 476 String bindDN = ""; 477 StringParameter bindDNParam = 478 parameters.getStringParameter(bindDNParameter.getName()); 479 if ((bindDNParam != null) && bindDNParam.hasValue()) 480 { 481 bindDN = bindDNParam.getStringValue(); 482 } 483 484 485 String bindPassword = ""; 486 PasswordParameter bindPWParam = 487 parameters.getPasswordParameter(bindPWParameter.getName()); 488 if ((bindPWParam != null) && bindPWParam.hasValue()) 489 { 490 bindPassword = bindPWParam.getStringValue(); 491 } 492 493 494 StringParameter baseDNParam = 495 parameters.getStringParameter(searchBaseParameter.getName()); 496 if ((baseDNParam == null) || (! baseDNParam.hasValue())) 497 { 498 outputMessages.add("ERROR: No base DN was provided."); 499 return false; 500 } 501 String baseDN = baseDNParam.getStringValue(); 502 503 504 LDAPConnection conn = new LDAPConnection(); 507 508 509 try 511 { 512 outputMessages.add("Attempting to establish a connection to " + host + 513 ":" + port + "...."); 514 conn.connect(host, port); 515 outputMessages.add("Connected successfully."); 516 outputMessages.add(""); 517 } 518 catch (Exception e) 519 { 520 outputMessages.add("ERROR: Unable to connect to the directory " + 521 "server: " + stackTraceToString(e)); 522 return false; 523 } 524 525 526 try 528 { 529 outputMessages.add("Attempting to perform an LDAPv3 bind to the " + 530 "directory server with a DN of '" + bindDN + "'...."); 531 conn.bind(3, bindDN, bindPassword); 532 outputMessages.add("Bound successfully."); 533 outputMessages.add(""); 534 } 535 catch (Exception e) 536 { 537 try 538 { 539 conn.disconnect(); 540 } catch (Exception e2) {} 541 542 outputMessages.add("ERROR: Unable to bind to the directory server: " + 543 stackTraceToString(e)); 544 return false; 545 } 546 547 548 try 550 { 551 outputMessages.add("Checking to make sure that the base DN entry '" + 552 baseDN + "' exists in the directory...."); 553 LDAPEntry baseDNEntry = conn.read(baseDN, new String [] { "1.1" }); 554 if (baseDNEntry == null) 555 { 556 try 557 { 558 conn.disconnect(); 559 } catch (Exception e2) {} 560 561 outputMessages.add("ERROR: Unable to retrieve the base DN entry."); 562 return false; 563 } 564 else 565 { 566 outputMessages.add("Successfully read the base DN entry."); 567 outputMessages.add(""); 568 } 569 } 570 catch (Exception e) 571 { 572 try 573 { 574 conn.disconnect(); 575 } catch (Exception e2) {} 576 577 outputMessages.add("ERROR: Unable to retrieve the base DN entry: " + 578 stackTraceToString(e)); 579 return false; 580 } 581 582 583 try 586 { 587 conn.disconnect(); 588 } catch (Exception e) {} 589 590 outputMessages.add("All tests completed successfully."); 591 return true; 592 } 593 594 595 596 607 public void initializeClient(String clientID, ParameterList parameters) 608 throws UnableToRunException 609 { 610 ldapHost = null; 612 hostParameter = parameters.getStringParameter(hostParameter.getName()); 613 if (hostParameter != null) 614 { 615 ldapHost = hostParameter.getStringValue(); 616 } 617 618 ldapPort = 389; 620 portParameter = parameters.getIntegerParameter(portParameter.getName()); 621 if (portParameter != null) 622 { 623 ldapPort = portParameter.getIntValue(); 624 } 625 626 bindDN = ""; 628 bindDNParameter = parameters.getStringParameter(bindDNParameter.getName()); 629 if (bindDNParameter != null) 630 { 631 bindDN = bindDNParameter.getStringValue(); 632 } 633 634 bindPassword = ""; 636 bindPWParameter = 637 parameters.getPasswordParameter(bindPWParameter.getName()); 638 if (bindPWParameter != null) 639 { 640 bindPassword = bindPWParameter.getStringValue(); 641 } 642 643 searchBase = ""; 645 searchBaseParameter = 646 parameters.getStringParameter(searchBaseParameter.getName()); 647 if (searchBaseParameter != null) 648 { 649 searchBase = searchBaseParameter.getStringValue(); 650 } 651 652 attributeName = null; 654 attributeNameParameter = 655 parameters.getStringParameter(attributeNameParameter.getName()); 656 if (attributeNameParameter != null) 657 { 658 attributeName = attributeNameParameter.getStringValue(); 659 } 660 661 rangeStart = 0; 663 rangeStartParameter = 664 parameters.getIntegerParameter(rangeStartParameter.getName()); 665 if (rangeStartParameter != null) 666 { 667 rangeStart = rangeStartParameter.getIntValue(); 668 } 669 670 rangeStop = Integer.MAX_VALUE; 672 rangeStopParameter = 673 parameters.getIntegerParameter(rangeStopParameter.getName()); 674 if (rangeStopParameter != null) 675 { 676 rangeStop = rangeStopParameter.getIntValue(); 677 } 678 679 680 timeLimit = 0; 682 timeLimitParameter = 683 parameters.getIntegerParameter(timeLimitParameter.getName()); 684 if (timeLimitParameter != null) 685 { 686 timeLimit = timeLimitParameter.getIntValue(); 687 } 688 689 numClients = 0; 691 clientsParameter = 692 parameters.getIntegerParameter(clientsParameter.getName()); 693 if (clientsParameter != null) 694 { 695 numClients = clientsParameter.getIntValue(); 696 } 697 698 699 int totalSpan = rangeStop - rangeStart + 1; 701 int spanPerClient = totalSpan / numClients; 702 if ((totalSpan % numClients) != 0) 703 { 704 spanPerClient++; 705 } 706 707 int clientNumber = getClientNumber(); 708 if (clientNumber >= numClients) 709 { 710 throw new UnableToRunException("Detected that this client is client " + 711 "number " + clientNumber + 712 ", but the reported number of clients " + 713 "was " + numClients + 714 " -- skipping this client"); 715 } 716 else if (clientNumber == (numClients - 1)) 717 { 718 clientMin = (clientNumber * spanPerClient) + rangeStart; 719 clientMax = rangeStop; 720 } 721 else 722 { 723 clientMin = (clientNumber * spanPerClient) + rangeStart; 724 clientMax = clientMin + spanPerClient - 1; 725 } 726 } 727 728 729 745 public void initializeThread(String clientID, String threadID, 746 int collectionInterval, ParameterList parameters) 747 throws UnableToRunException 748 { 749 entryCount = new IntegerValueTracker(clientID, threadID, 751 STAT_TRACKER_ENTRY_COUNT, 752 collectionInterval); 753 exceptionsCaught = new IncrementalTracker(clientID, threadID, 754 STAT_TRACKER_EXCEPTIONS_CAUGHT, 755 collectionInterval); 756 searchTime = new TimeTracker(clientID, threadID, STAT_TRACKER_SEARCH_TIME, 757 collectionInterval); 758 successfulSearches = new IncrementalTracker(clientID, threadID, 759 STAT_TRACKER_SEARCHES_COMPLETED, 760 collectionInterval); 761 totalSearches = 762 new AccumulatingTracker(clientID, threadID, 763 STAT_TRACKER_TOTAL_SEARCHES_COMPLETED, 764 collectionInterval); 765 766 767 RealTimeStatReporter statReporter = getStatReporter(); 769 if (statReporter != null) 770 { 771 String jobID = getJobID(); 772 successfulSearches.enableRealTimeStats(statReporter, jobID); 773 totalSearches.enableRealTimeStats(statReporter, jobID); 774 exceptionsCaught.enableRealTimeStats(statReporter, jobID); 775 entryCount.enableRealTimeStats(statReporter, jobID); 776 searchTime.enableRealTimeStats(statReporter, jobID); 777 } 778 779 780 int numThreads = getClientSideJob().getThreadsPerClient(); 782 int clientSpan = clientMax - clientMin + 1; 783 int spanPerThread = clientSpan / numThreads; 784 if ((clientSpan % numThreads) != 0) 785 { 786 spanPerThread++; 787 } 788 789 int threadNumber = getThreadNumber(); 790 if (threadNumber == (numThreads - 1)) 791 { 792 threadMin = (threadNumber * spanPerThread) + clientMin; 793 threadMax = clientMax; 794 } 795 else 796 { 797 threadMin = (threadNumber * spanPerThread) + clientMin; 798 threadMax = threadMin + spanPerThread - 1; 799 } 800 } 801 802 803 804 811 public void runJob() 812 { 813 conn = new LDAPConnection(); 815 try 816 { 817 conn.connect(3, ldapHost, ldapPort, bindDN, bindPassword); 818 } 819 catch (LDAPException le) 820 { 821 logMessage("Unable to connect to directory server " + ldapHost + ":" + 822 ldapPort + ": " + le); 823 indicateCompletedWithErrors(); 824 return; 825 } 826 827 LDAPSearchConstraints searchConstraints = conn.getSearchConstraints(); 829 searchConstraints.setTimeLimit(1000 * timeLimit); 830 searchConstraints.setServerTimeLimit(timeLimit); 831 832 833 successfulSearches.startTracker(); 835 totalSearches.startTracker(); 836 exceptionsCaught.startTracker(); 837 entryCount.startTracker(); 838 searchTime.startTracker(); 839 840 for (int i=threadMin; ((! shouldStop()) && (i <= threadMax)); i++) 842 { 843 boolean successfulSearch = false; 846 847 848 int matchingEntries = 0; 851 852 searchTime.startTimer(); 854 try 855 { 856 String filter = "(" + attributeName + "=" + i + ")"; 857 LDAPSearchResults results = conn.search(searchBase, 858 LDAPConnection.SCOPE_SUB, 859 filter, null, false, 860 searchConstraints); 861 while (results.hasMoreElements()) 862 { 863 if (results.nextElement() instanceof LDAPEntry) 864 { 865 matchingEntries++; 866 } 867 } 868 869 successfulSearch = true; 870 } 871 catch (Exception e) 872 { 873 exceptionsCaught.increment(); 874 indicateCompletedWithErrors(); 875 } 876 877 searchTime.stopTimer(); 879 880 881 if (successfulSearch) 883 { 884 entryCount.addValue(matchingEntries); 885 successfulSearches.increment(); 886 totalSearches.increment(); 887 } 888 } 889 890 891 try 893 { 894 conn.disconnect(); 895 } catch (LDAPException le) {} 896 897 successfulSearches.stopTracker(); 899 totalSearches.stopTracker(); 900 exceptionsCaught.stopTracker(); 901 entryCount.stopTracker(); 902 searchTime.stopTracker(); 903 } 904 905 906 907 911 public void destroy() 912 { 913 if (conn != null) 914 { 915 try 916 { 917 conn.disconnect(); 918 } catch (Exception e) {} 919 920 conn = null; 921 } 922 } 923 } 924 925 | Popular Tags |