Esempio n. 1
0
 protected void _deleteDependencies() throws DBException {
   DBField[] keyFlds = this.getKeyFields();
   DBFieldValues fldVals = this.getFieldValues();
   DBFactory fact = this.getFactory();
   DBFactory childFact[] = fact.getChildFactories();
   for (int i = 0; i < childFact.length; i++) {
     DBRecordKey key = childFact[i].createKey(); // an empty key
     for (int k = 0; k < keyFlds.length; k++) {
       String fldName = keyFlds[k].getName();
       if (fldVals.hasFieldValue(fldName)) {
         Object fldValue = fldVals.getFieldValue(fldName);
         key.setFieldValue(fldName, fldValue);
       } else {
         throw new DBException("Missing dependent key fields!");
       }
     }
     // Do not perform recursive dependency deletion!
     // - 'key' is an incomplete (partial key only), and dependency deletion would fail
     // - all dependent children should already be specified by "getChildFactories()"
     try {
       int whereKeyType = DBWhere.KEY_PARTIAL_ALL; // Should use ALL available partial keys.
       key._delete(null, whereKeyType); // primary key delete
     } catch (SQLException sqe) {
       throw new DBException("Record deletion", sqe);
     }
   }
 }
Esempio n. 2
0
 public static DBFactory<AccountString> getFactory() {
   if (factory == null) {
     factory =
         DBFactory.createDBFactory(
             AccountString.TABLE_NAME(),
             AccountString.FieldInfo,
             DBFactory.KeyType.PRIMARY,
             AccountString.class,
             AccountString.Key.class,
             true /*editable*/,
             true /*viewable*/);
     factory.addParentTable(Account.TABLE_NAME());
   }
   return factory;
 }
Esempio n. 3
0
 public static DBFactory<GroupList> getFactory() {
   if (factory == null) {
     factory =
         DBFactory.createDBFactory(
             GroupList.TABLE_NAME(),
             GroupList.FieldInfo,
             DBFactory.KeyType.PRIMARY,
             GroupList.class,
             GroupList.Key.class,
             true /*editable*/,
             true /*viewable*/);
     factory.addParentTable(Account.TABLE_NAME());
     factory.addParentTable(User.TABLE_NAME());
     factory.addParentTable(DeviceGroup.TABLE_NAME());
   }
   return factory;
 }
Esempio n. 4
0
 public static DBFactory<StatusCode> getFactory() {
   if (factory == null) {
     factory =
         DBFactory.createDBFactory(
             StatusCode.TABLE_NAME(),
             StatusCode.FieldInfo,
             DBFactory.KeyType.PRIMARY,
             StatusCode.class,
             StatusCode.Key.class,
             true /*editable*/,
             true /*viewable*/);
     factory.addParentTable(Account.TABLE_NAME());
     factory.addParentTable(Device.TABLE_NAME());
     factory.setFieldDefaultValue(FLD_deviceID, ALL_DEVICES);
   }
   return factory;
 }
Esempio n. 5
0
 public static DBFactory<RoleAcl> getFactory() {
   if (factory == null) {
     EnumTools.registerEnumClass(AccessLevel.class);
     factory =
         DBFactory.createDBFactory(
             RoleAcl.TABLE_NAME(),
             RoleAcl.FieldInfo,
             DBFactory.KeyType.PRIMARY,
             RoleAcl.class,
             RoleAcl.Key.class,
             true /*editable*/,
             true /*viewable*/);
     factory.addParentTable(Account.TABLE_NAME());
     factory.addParentTable(Role.TABLE_NAME());
   }
   return factory;
 }
Esempio n. 6
0
  /**
   * ** Gets a virtual DBRecord from the specified remote service ** @param servReq The remote web
   * service ** @return The virtual DBRecord (cannot be saved or reloaded)
   */
  @SuppressWarnings("unchecked")
  public gDBR getVirtualDBRecord(final ServiceRequest servReq) throws DBException {
    String CMD_dbget = DBFactory.CMD_dbget;
    String TAG_Response = servReq.getTagResponse();
    String TAG_Record = DBFactory.TAG_Record;
    String ATTR_command = servReq.getAttrCommand();
    String ATTR_result = servReq.getAttrResult();

    /* send request / get response */
    Document xmlDoc = null;
    try {
      xmlDoc =
          servReq.sendRequest(
              CMD_dbget,
              new ServiceRequest.RequestBody() {
                public StringBuffer appendRequestBody(StringBuffer sb, int indent) {
                  return DBRecordKey.this.toRequestXML(sb, indent);
                }
              });
    } catch (IOException ioe) {
      Print.logException("Error", ioe);
      throw new DBException("Request read error", ioe);
    }

    /* parse 'GTSResponse' */
    Element gtsResponse = xmlDoc.getDocumentElement();
    if (!gtsResponse.getTagName().equalsIgnoreCase(TAG_Response)) {
      Print.logError("Request XML does not start with '%s'", TAG_Response);
      throw new DBException("Response XML does not begin eith '" + TAG_Response + "'");
    }

    /* request command/argument */
    String cmd = StringTools.trim(gtsResponse.getAttribute(ATTR_command));
    String result = StringTools.trim(gtsResponse.getAttribute(ATTR_result));
    if (StringTools.isBlank(result)) {
      result = StringTools.trim(gtsResponse.getAttribute("type"));
    }
    if (!result.equalsIgnoreCase("success")) {
      Print.logError("Response indicates failure");
      throw new DBException("Response does not indicate 'success'");
    }

    /* Record */
    NodeList rcdList = XMLTools.getChildElements(gtsResponse, TAG_Record);
    if (rcdList.getLength() <= 0) {
      Print.logError("No 'Record' tags");
      throw new DBException("GTSResponse does not contain any 'Record' tags");
    }
    Element rcdElem = (Element) rcdList.item(0);

    /* return DBRecord */
    gDBR dbr = (gDBR) DBFactory.parseXML_DBRecord(rcdElem);
    dbr.setVirtual(true);
    return dbr;
  }
Esempio n. 7
0
  /**
   * ** Traverses the DBFactory dependency tree, creating a DBFactoryTree ** @param level The
   * current tree level ** @param dbFact The current DBFactory to add ** @param parentNode The
   * parent node to which a new DBFactoryNode child will be added ** @param addedTables A set of
   * table names added to the current DBFactoryTree *
   */
  private static void _traverseDBFactoryTree(
      int level,
      DBFactory<? extends DBRecord> dbFact,
      DBFactoryTree parentNode,
      Set<String> addedTables) {

    /* no DBFactory? */
    if (dbFact == null) {
      Print.logError("Null DBFactory!");
      return;
    }
    String utableName = dbFact.getUntranslatedTableName();

    /* already added? */
    if (addedTables.contains(utableName)) {
      return;
    }
    addedTables.add(utableName);

    /* add this node */
    // Print.logInfo(StringTools.replicateString("  ",level) + dbFact.getUntranslatedTableName());
    DBFactoryTree dbFactNode = new DBFactoryTree(level, parentNode, dbFact);
    parentNode.addChild(dbFactNode);

    /* find dependent children */
    DBFactory<? extends DBRecord> childFact[] = dbFact.getChildFactories();
    for (int i = 0; i < childFact.length; i++) {
      int index = childFact[i].getParentTables().indexOf(utableName);
      if (level == index) {
        DBFactoryTree._traverseDBFactoryTree(level + 1, childFact[i], dbFactNode, addedTables);
      } else if (!addedTables.contains(childFact[i].getUntranslatedTableName())) {
        Print.logWarn(
            "Skipping table in heiarchy: "
                + utableName
                + " ==> "
                + childFact[i].getUntranslatedTableName());
      }
    }
  }
Esempio n. 8
0
  /**
   * ** Returns true if the parent records in their respective parent tables exist. ** @return True
   * if the parent records exist.
   */
  public boolean parentsExist() throws DBException {
    DBFactory<gDBR> dbFact = this.getFactory();
    DBFieldValues myFldVals = this.getFieldValues();
    java.util.List<String> parentList = dbFact.getParentTables();
    for (String parentTable : parentList) {

      /* get parent table DBFactory */
      Print.logInfo("[%s] Parent table: %s", this.getTableName(), parentTable);
      DBFactory parentFact = DBFactory.getFactoryByName(parentTable);
      if (parentFact == null) {
        Print.logError("Unexpected error finding parent table: " + parentTable);
        return false;
      }

      /* create parent record key with fields from this key */
      DBRecordKey parentKey = parentFact.createKey(); // an empty key
      DBField parentKeyFlds[] = parentFact.getKeyFields();
      for (DBField pkf : parentKeyFlds) {
        String pfn = pkf.getName();

        /* get this DBField */
        DBField myKeyFld = this.getField(pfn);
        if (myKeyFld == null) {
          Print.logError("Unexpected error finding field: [" + this.getTableName() + "] " + pfn);
          return false;
        }

        /* get parent key field value */
        Object pkv = myFldVals.getFieldValue(pfn);
        if (pkv == null) {
          Print.logError("Unexpected error finding parent field: [" + parentTable + "] " + pfn);
          return false;
        }
        if (myKeyFld.isDefaultValue(pkv)) {
          Print.logInfo("This key contains a global value, skipping parent check: " + parentTable);
          parentKey = null;
          break;
        }
        parentKey.setFieldValue(pfn, pkv);
      }

      /* check parent existence */
      if ((parentKey != null) && !parentKey.exists()) {
        Print.logError("Parent record does not exist: [" + parentTable + "] " + parentKey);
        return false;
      }
    }
    return true;
  }
Esempio n. 9
0
 /** ** Encodes this DBRecordKey into XML for "GTSRequest' purposes */
 private StringBuffer toRequestXML(StringBuffer sb, int indent) {
   boolean isSoapReq = false;
   if (sb == null) {
     sb = new StringBuffer();
   }
   DBRecordKey<gDBR> recKey = this;
   String tableName = recKey.getTableName();
   DBField fld[] = recKey.getKeyFields(); // KEY fields
   DBFieldValues fldVals = recKey.getFieldValues();
   String PFX1 = XMLTools.PREFIX(isSoapReq, indent);
   sb.append(PFX1);
   sb.append(
       XMLTools.startTAG(
           isSoapReq,
           DBFactory.TAG_Record,
           XMLTools.ATTR(DBFactory.ATTR_table, tableName),
           false,
           true));
   DBFactory.writeXML_DBFields(sb, 2 * indent, fld, fldVals, isSoapReq);
   sb.append(PFX1);
   sb.append(XMLTools.endTAG(isSoapReq, DBFactory.TAG_Record, true));
   return sb;
 }
Esempio n. 10
0
 /**
  * ** Encodes this DBRecordKey into XML ** @param sb The StringBuffer to which the DBRecord XML is
  * writen ** @param indent The number of spaces to indent ** @param sequence An optional record
  * sequence number ** @param soapXML True for SOAP XML ** @return The StringBuffer
  */
 public StringBuffer toXML(StringBuffer sb, int indent, int sequence, boolean soapXML) {
   if (sb == null) {
     sb = new StringBuffer();
   }
   String prefix = StringTools.replicateString(" ", indent);
   DBRecordKey<gDBR> recKey = this;
   String tableName = recKey.getTableName();
   DBField fld[] = recKey.getKeyFields(); // KEY fields
   DBFieldValues fldVals = recKey.getFieldValues();
   String PFX1 = XMLTools.PREFIX(soapXML, indent);
   sb.append(PFX1);
   sb.append(
       XMLTools.startTAG(
           soapXML,
           DBFactory.TAG_RecordKey,
           XMLTools.ATTR(DBFactory.ATTR_table, tableName)
               + ((sequence > 0) ? XMLTools.ATTR(DBFactory.ATTR_sequence, sequence) : ""),
           false,
           true));
   DBFactory.writeXML_DBFields(sb, 2 * indent, fld, fldVals, soapXML);
   sb.append(PFX1);
   sb.append(XMLTools.endTAG(soapXML, DBFactory.TAG_RecordKey, true));
   return sb;
 }
Esempio n. 11
0
  @SuppressWarnings("unchecked")
  public static void main(String[] args) {
    String dbname;
    Properties props = new Properties();
    Properties fileprops = new Properties();
    boolean dotransactions = true;
    int threadcount = 1;
    int target = 0;
    boolean status = false;
    String label = "";

    // parse arguments
    int argindex = 0;

    if (args.length == 0) {
      usageMessage();
      System.exit(0);
    }

    while (args[argindex].startsWith("-")) {
      if (args[argindex].compareTo("-threads") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        int tcount = Integer.parseInt(args[argindex]);
        props.setProperty("threadcount", tcount + "");
        argindex++;
      } else if (args[argindex].compareTo("-target") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        int ttarget = Integer.parseInt(args[argindex]);
        props.setProperty("target", ttarget + "");
        argindex++;
      } else if (args[argindex].compareTo("-load") == 0) {
        dotransactions = false;
        argindex++;
      } else if (args[argindex].compareTo("-t") == 0) {
        dotransactions = true;
        argindex++;
      } else if (args[argindex].compareTo("-s") == 0) {
        status = true;
        argindex++;
      } else if (args[argindex].compareTo("-db") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        props.setProperty("db", args[argindex]);
        argindex++;
      } else if (args[argindex].compareTo("-l") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        label = args[argindex];
        argindex++;
      } else if (args[argindex].compareTo("-P") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        String propfile = args[argindex];
        argindex++;

        Properties myfileprops = new Properties();
        try {
          myfileprops.load(new FileInputStream(propfile));
        } catch (IOException e) {
          System.out.println(e.getMessage());
          System.exit(0);
        }

        // Issue #5 - remove call to stringPropertyNames to make compilable under Java 1.5
        for (Enumeration e = myfileprops.propertyNames(); e.hasMoreElements(); ) {
          String prop = (String) e.nextElement();

          fileprops.setProperty(prop, myfileprops.getProperty(prop));
        }

      } else if (args[argindex].compareTo("-p") == 0) {
        argindex++;
        if (argindex >= args.length) {
          usageMessage();
          System.exit(0);
        }
        int eq = args[argindex].indexOf('=');
        if (eq < 0) {
          usageMessage();
          System.exit(0);
        }

        String name = args[argindex].substring(0, eq);
        String value = args[argindex].substring(eq + 1);
        props.put(name, value);
        // System.out.println("["+name+"]=["+value+"]");
        argindex++;
      } else {
        System.out.println("Unknown option " + args[argindex]);
        usageMessage();
        System.exit(0);
      }

      if (argindex >= args.length) {
        break;
      }
    }

    if (argindex != args.length) {
      usageMessage();
      System.exit(0);
    }

    // set up logging
    // BasicConfigurator.configure();

    // overwrite file properties with properties from the command line

    // Issue #5 - remove call to stringPropertyNames to make compilable under Java 1.5
    for (Enumeration e = props.propertyNames(); e.hasMoreElements(); ) {
      String prop = (String) e.nextElement();

      fileprops.setProperty(prop, props.getProperty(prop));
    }

    props = fileprops;

    if (!checkRequiredProperties(props)) {
      System.exit(0);
    }

    long maxExecutionTime = Integer.parseInt(props.getProperty(MAX_EXECUTION_TIME, "0"));

    // get number of threads, target and db
    threadcount = Integer.parseInt(props.getProperty("threadcount", "1"));
    dbname = props.getProperty("db", "com.yahoo.ycsb.BasicDB");
    target = Integer.parseInt(props.getProperty("target", "0"));

    // compute the target throughput
    double targetperthreadperms = -1;
    if (target > 0) {
      double targetperthread = ((double) target) / ((double) threadcount);
      targetperthreadperms = targetperthread / 1000.0;
    }

    System.out.println("YCSB Client 0.1");
    System.out.print("Command line:");
    for (int i = 0; i < args.length; i++) {
      System.out.print(" " + args[i]);
    }
    System.out.println();
    System.err.println("Loading workload...");

    // show a warning message that creating the workload is taking a while
    // but only do so if it is taking longer than 2 seconds
    // (showing the message right away if the setup wasn't taking very long was confusing people)
    Thread warningthread =
        new Thread() {
          public void run() {
            try {
              sleep(2000);
            } catch (InterruptedException e) {
              return;
            }
            System.err.println(" (might take a few minutes for large data sets)");
          }
        };

    warningthread.start();

    // set up measurements
    Measurements.setProperties(props);

    // load the workload
    ClassLoader classLoader = Client.class.getClassLoader();

    Workload workload = null;

    try {
      Class workloadclass = classLoader.loadClass(props.getProperty(WORKLOAD_PROPERTY));

      workload = (Workload) workloadclass.newInstance();
    } catch (Exception e) {
      e.printStackTrace();
      e.printStackTrace(System.out);
      System.exit(0);
    }

    try {
      workload.init(props);
    } catch (WorkloadException e) {
      e.printStackTrace();
      e.printStackTrace(System.out);
      System.exit(0);
    }

    warningthread.interrupt();

    // run the workload

    System.err.println("Starting test.");

    int opcount;
    if (dotransactions) {
      opcount = Integer.parseInt(props.getProperty(OPERATION_COUNT_PROPERTY, "0"));
    } else {
      if (props.containsKey(INSERT_COUNT_PROPERTY)) {
        opcount = Integer.parseInt(props.getProperty(INSERT_COUNT_PROPERTY, "0"));
      } else {
        opcount = Integer.parseInt(props.getProperty(RECORD_COUNT_PROPERTY, "0"));
      }
    }

    Vector<Thread> threads = new Vector<Thread>();

    for (int threadid = 0; threadid < threadcount; threadid++) {
      DB db = null;
      try {
        db = DBFactory.newDB(dbname, props);
      } catch (UnknownDBException e) {
        System.out.println("Unknown DB " + dbname);
        System.exit(0);
      }

      Thread t =
          new ClientThread(
              db,
              dotransactions,
              workload,
              threadid,
              threadcount,
              props,
              opcount / threadcount,
              targetperthreadperms);

      threads.add(t);
      // t.start();
    }

    StatusThread statusthread = null;

    if (status) {
      boolean standardstatus = false;
      if (props.getProperty("measurementtype", "").compareTo("timeseries") == 0) {
        standardstatus = true;
      }
      statusthread = new StatusThread(threads, label, standardstatus);
      statusthread.start();
    }

    long st = System.currentTimeMillis();

    for (Thread t : threads) {
      t.start();
    }

    Thread terminator = null;

    if (maxExecutionTime > 0) {
      terminator = new TerminatorThread(maxExecutionTime, threads, workload);
      terminator.start();
    }

    int opsDone = 0;

    for (Thread t : threads) {
      try {
        t.join();
        opsDone += ((ClientThread) t).getOpsDone();
      } catch (InterruptedException e) {
      }
    }

    long en = System.currentTimeMillis();

    if (terminator != null && !terminator.isInterrupted()) {
      terminator.interrupt();
    }

    if (status) {
      statusthread.interrupt();
    }

    try {
      workload.cleanup();
    } catch (WorkloadException e) {
      e.printStackTrace();
      e.printStackTrace(System.out);
      System.exit(0);
    }

    try {
      exportMeasurements(props, opsDone, en - st);
    } catch (IOException e) {
      System.err.println("Could not export measurements, error: " + e.getMessage());
      e.printStackTrace();
      System.exit(-1);
    }

    System.exit(0);
  }