GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||
GeezerVison is showing docs.voltactivedata.com — 9.2. Modifying the Procedure Call and Arguments Guide to Performance and Customization9.2. Modifying the Procedure Call and ArgumentsTo vary the stored procedure called by a task or the arguments to that procedure, you can create a custom task that implements the ActionGenerator interface. The custom task consists of the Java class you create, and the SQL statements to load and declare the task. At run time, for each invocation of the task, VoltDB uses the custom class to determine what procedure to call and its arguments and the task declaration to determine when to run the task. Let's look at an example. Say we want a task that periodically deletes unused sessions. The stored procedure to do this might look like the following, where the batch size — the maximum number of records to delete — is passed in as an argument to the procedure: CREATE PROCEDURE PurgeOldSessions DIRECTED AS
DELETE FROM session
WHERE last_access < DATEADD(MINUTE,-5,NOW())
ORDER BY sessionID ASC LIMIT ?;We could schedule this procedure using a static task and a fixed batch size. But then there is no guarantee that the task can keep up with the volume of expired sessions. Instead, we would like to be able to adjust the batch size to accommodate changing workloads. To do this we can create a custom task that checks how many records were actually deleted in each run. If the count of deleted records is less than 80% of the batch size, reduce the batch size for the next run. On the other hand, if the number deleted equals the batch size, increase the batch size incrementally until it matches the workload. Finally, we can use parameters to the custom task to specify the minimum, maximum, and starting batch size. 9.2.1. Designing a Java Class That Implements ActionGeneratorThe first step is to write a Java class that implements the ActionGenerator interface. This must be a static class whose constructor takes no arguments. In our example, we will also declare class variables for a helper object that is used in subsequent methods plus minimum, maximum, and current values for the batch size. The class must, at a minimum, declare or override three methods:
Example 9.1, “Custom Task Implementing ActionGenerator” shows the completed example task class, with the key elements highlighted. Example 9.1. Custom Task Implementing ActionGenerator package mytasks;
import java.util.concurrent.TimeUnit;
import org.voltdb.VoltTable;
import org.voltdb.client.ClientResponse;
import org.voltdb.task.*;
public class PurgeBatches implements ActionGenerator {
private TaskHelper helper;
private long min, max, batchsize;
public void initialize(TaskHelper helper, 9.2.2. Compiling and Loading the Class into VoltDBOnce you complete your Java source code, you need to compile, debug, and package it into a JAR file so it can be
loaded into VoltDB. You compile and package task classes the same way you compile and package stored procedures. In fact,
you can package tasks and procedures into the same or separate JARs if you choose. The following example compiles the Java
classes in the $ javac -classpath "/opt/voltdb/voltdb/*" \
-d ./obj src/*.java
$ jar cvf sessiontasks.jar -C obj . You then load the classes from the JAR file into VoltDB using the sqlcmd LOAD CLASSES directive: LOAD CLASSES sessiontasks.jar; 9.2.3. Declaring the TaskFinally, once the custom class is loaded into the database, you can declare the task and start it running. You declare the task using the CREATE TASK statement, replacing a procedure name with the FROM CLASS clause specifying the classpath to your new class. In our example, the custom task also requires three arguments: a minimum, maximum and starting batch size. Because the stored procedure that the custom class specifies is a directed procedure (that is, it runs separately on every partition on the cluster), the task must be declared to RUN ON PARTITIONS. If the procedure was not directed, the task could be run on PARTITIONS, DATABASE, or HOSTS. However, for partitioned tables it is often necessary to have partitioned or directed procedures if the procedure's statements need to both ORDER BY and LIMIT the rows. The following statement creates the custom task with a minimum batch size of 100 records, a maximum of 2,000, and a starting size of 1,000. CREATE TASK batchcleanup
ON SCHEDULE EVERY 5 SECONDS
PROCEDURE FROM CLASS mytasks.PurgeBatches
WITH (100,2000,1000)
RUN ON PARTITIONS;Note that the task starts as soon as it is declared, unless you include the DISABLE clause. Alternately, you can use the ALTER TASK statement to change the state of the task. For example, the following statement disables our newly created task: ALTER TASK batchcleanup DISABLE; | ||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |