As useful as scheduling regular stored procedures is in simplifying application development, it can be disruptive to ongoing workflow if multi-partition procedures take too long or run too frequently. It would be nice to be able to schedule some partitioned activities as well to do piecemeal work on each partition without tying up all of the partitions at once. This is exactly what directed procedures are designed to do.
A directed procedure is a special type of stored procedure, declared using the DIRECTED clause instead of PARTITION ON. You write a directed procedure the same way you write a regular stored procedure: either as a simple procedure of one or more SQL statements or as a Java class extending voltProcedure, using the voltQueueSQL method to queue SQL statements. Since it is transactional, the procedure must also be deterministic.
However, if you declare the procedure as DIRECTED, when you invoke it a separate instance of the procedure is queued on every partition in the database. Each instance is its own transaction and acts like a partitioned procedure. So the separate transactions do not block the other partitions. However, because they are separate, there is no coordination between the transactions and no guarantee that they are executed at the same time.
This makes directed procedures particularly useful for non-critical procedures that need to access data across the database but do not need to be coordinated as a single, atomic transaction. Because of the special nature of directed procedures, you cannot invoke them the way you would normal partitioned or multi-partitioned procedures. Instead, the primary way to invoke them is as a scheduled task.
To schedule a directed procedure as a task, you use the same syntax for the CREATE TASK statement as for a multi-partitioned procedure, except you add the RUN ON PARTITIONS clause. The RUN ON PARTITIONS clause specifies that the task is scheduled separately for each and every partition. For example, if you want to run the RemoveOrphans task defined in the previous section as a directed procedure so it will not block the ongoing database workload, you would add the DIRECTED clause to the CREATE PROCEDURE statement and the RUN ON PARTITIONS clause to the CREATE TASK statement, like so:
CREATE PROCEDURE OrphanedRecords DIRECTED AS DELETE FROM reservations WHERE airline=? AND flight_id IS NULL; CREATE TASK RemoveOrphans ON SCHEDULE EVERY 2 HOURS PROCEDURE OrphanedRecords WITH ('FlyByNight') RUN ON PARTITIONS;
Although scheduled tasks are the easiest way to invoke directed procedures, you can also invoke them directly from
your Java applications. You cannot call them with the
callProcedure method, but you can using the
callAllPartitionProcedure method where the results from all of the partitions are returned as an array of
VoltTables, one per partition. See the descriptions of the CREATE PROCEDURE AS,
CREATE PROCEDURE FROM CLASS, and CREATE
TASK statements for more information about using directed procedures.