Heartbeat Merge Sample

This sample uses two Heartbeat operators to prevent a Merge operator from starving when either of its inputs stop receiving tuples. Without the Heartbeat operators, the Merge operator would stop outputting tuples as soon as either of its two input streams is inactive, even if it continued to receive tuples on the other input stream.

This sample also demonstrates how to flag the non-heartbeat tuples on an input stream, so that the heartbeat tuples can be filtered out later on.

Importing This Sample into StreamBase Studio

In StreamBase Studio, import this sample with the following steps:

  • From the top menu, click FileLoad StreamBase Sample.

  • Select operator from the Applications list.

  • Click OK.

StreamBase Studio creates a single project for the operator samples.

Sample Location

By default, the sample files are installed in:

  • On Windows: C:\Program Files\StreamBase Systems\StreamBase.n.m\sample\operator

  • On UNIX: /opt/streambase/sample/operator

When you load the sample into StreamBase Studio, Studio copies the sample project's files to your Studio workspace. StreamBase Systems recommends that you use the workspace copy of the sample, especially on UNIX, where you may not have write access to /opt/streambase. In the default installation, the path to this sample in your Studio workspace is:

UNIX:       
  ~/streambase-studio-n.m-workspace/sample_operator
Windows XP:
  C:\Documents and Settings\username\My Documents\StreamBase Studio n.m Workspace\
      sample_operator
Windows Vista:
  C:\Users\username\Documents\StreamBase Studio n.m Workspace\
      sample_operator

Running the Heartbeat Merge Sample

Running HeartbeatMerge.sbapp in StreamBase Studio

  1. In the Package Explorer, double-click to open the HeartbeatMerge.sbapp application. Make sure the application is the currently active tab in the EventFlow Editor.

  2. Click the Run button. This opens the SB Test/Debug perspective and starts the application.

  3. In the Application Output view, select the MergedFeeds stream. No output is displayed at this point, but the Output View is prepared to receive output. This view will eventually show the output of the application.

  4. In the Manual Input view, select the Feed1 input stream.

  5. Enter IBM for symbol, 83.17 for price, and 2005-11-02 12:00:00 for time.

  6. Click Send Data. You should not see any output in the Output View yet. The tuple you just sent is currently buffered in the Merge operator.

  7. Select the Feed2 input stream.

  8. Enter MSFT for symbol, 24.89 for price, and 2005-11-02 12:00:30 for time.

  9. Click Send Data. You should now see the IBM tuple in Output View.

  10. Wait a few moments. Within 30 seconds, look for the MSFT tuple to appear in the Output View. If this application had not made use of Heartbeat operators, the MSFT tuple would have remained inside the Merge operator until Feed1 received a tuple with a timestamp greater than or equal to 2005-11-02 12:00:30. Instead, the Heartbeat operators continually emit such tuples, thereby preventing the Merge operator from getting stuck. You can see this for yourself by selecting the Feed1WithHeartbeats output stream in the Output View.

  11. When done, press F9 or click the Stop Running Application button.

Running HeartbeatAggregate.sbapp in Terminal Windows

This section describes how to run the sample in UNIX terminal windows or Windows command prompt windows. On Windows, be sure to use the StreamBase Command Prompt from the Start menu as described in the Test/Debug Guide, not the default command prompt.

  1. Open three terminal windows on UNIX, or three StreamBase Command Prompts on Windows. In each window, navigate to the directory where the sample is installed, or to your workspace copy of the sample, as described above.

  2. In window 1, type:

    sbd HeartbeatAggregate.sbapp

    The window shows notice[StreamBaseServer] listening on port 10000.

  3. In window 2, type:

    sbc dequeue TicCounts

    No output is displayed at this point, but the dequeuer is prepared to receive output of the application.

  4. In window 3, type:

    echo IBM,84.17,2005-11-02 12:00:00 | sbc enqueue Feed1

    You should not see any output in the dequeuer window yet. The tuple you just sent is currently buffered in the Merge operator.

  5. In window 3, type:

    echo MSFT,24.89,2005-11-02 12:00:30 | sbc enqueue Feed2

    Wait a few moments. Within 30 seconds, look for the MSFT tuple to appear in the dequeuer window. If this application had not made use of Heartbeat operators, the MSFT tuple would have remained stuck inside the Merge operator until Feed1 received a tuple with a timestamp greater than or equal to 2005-11-02 12:00:30. Instead, the Heartbeat operators continually emit such tuples, thereby preventing the Merge operator from getting stuck. You can see this for yourself by dequeuing the Feed1WithHeartbeats output stream.

  6. In window 2, type: Ctrl-Z (Windows) or Ctrl-D (UNIX) to exit the sbc session.

  7. In window 3, type the following command to terminate the server and dequeuer:

    sbadmin shutdown

Back to Top ^

How We Created the Heartbeat Merge Sample

  1. Launched StreamBase Studio

  2. Created (or subsequently used) the sample_operator project.

  3. From the top menu, in the SB Authoring perspective, selected FileNewEventFlow Application. Selected the sample_operator project and entered HeartbeatMerge for the diagram name.

  4. Added two input streams:

    1. Dragged an input stream from the palette to the canvas.

    2. Clicked the new input stream to open its properties.

    3. On the Input Stream Properties view General tab, added Name: Feed1

    4. On the Edit Schema tab, added:

      • Field Name: symbol, Type: string, Size: 4

      • Field Name: price, Type: double, Size: 8

      • Field Name: time, Type: timestamp, Size: 8

    5. On the canvas, copied and pasted Feed1 to create the second input stream below it.

    6. Selected the new input stream. On the Properties view's General tab, changed the input stream name to Feed2.

  5. Added two Map operators:

    1. Dragged a Map operator from the palette to the canvas.

    2. Dragged a connector from the Feed1 input stream to the Map operator.

    3. Clicked the new Map operator. On the Map Properties view General tab, added Name: TagDataTuples1

    4. On the Output Settings tab:

      1. Selected the All input fields with specified changes option.

      2. Clicked the Add button and added the following Output Fields row:

        Action: Add, Field Name: isDataTuple, Expression: true

    5. Copied and pasted TagDataTuples1 to create the second Map operator below it.

    6. Dragged a connector from the Feed2 input stream to the TagDataTuples2 Map operator.

    7. Selected the new Map operator. On the Properties view General tab, changed the operator name to TagDataTuples2.

  6. Added two Heartbeat operators:

    1. Dragged a Heartbeat operator from the palette to the canvas.

    2. Connected the TagDataTuples1 Map operator to the new Heartbeat operator.

    3. Clicked the Heartbeat operator. On the Properties view General tab, added Name: Heartbeat1

    4. On the Output Settings tab, added:

      • Tuple output interval: 0.5 (seconds)

      • Input tuple timestamp field: time

      • Maximum delay: 2 (seconds)

    5. Copied and pasted Heartbeat1 to create the second Heartbeat operator below it.

    6. Connected the TagDataTuples2 Map operator to the Heartbeat2 operator.

    7. Selected the new Heartbeat operator. On the Properties view's General tab, changed the operator name to Heartbeat2.

  7. Added a Merge operator:

    1. Dragged a Merge operator from the palette to the canvas. Placed it after the Heartbeat2 operator.

    2. Connected the Heartbeat1 operator to the top input (left) port of the Merge operator.

    3. Connected the Heartbeat2 operator to the bottom input port of the Merge operator.

    4. Selected the Merge operator. On the Properties view's Merge Settings tab, added Field by which to merge tuples: time.

  8. Added a Filter operator:

    1. Dragged a Filter operator from the palette to the canvas, after the Merge1 operator.

    2. Connected the Merge1 operator to the Filter operator.

    3. Selected the Filter operator. On the Properties view's General tab, added Name: DropHeartbeatTuples.

    4. On the Predicate Settings tab, clicked the Add button and added the following Predicates row:

      Output Port: 1, Predicate: isDataTuple

  9. Added two output streams:

    1. Dragged an output stream from the palette to the canvas, placing it after the Heartbeat1 operator.

    2. Drew a second connection from the Heartbeat1 operator to the output stream (adding to the existing connection from Heartbeat1 to Merge1).

    3. Selected the new output stream. On the Properties view's General tab, added Name: Feed1WithHeartbeats

    4. Dragged a second output stream from the palette to the canvas, placing it after the DropHeartbeatTuples Filter operator.

    5. Connected the Filter operator to the second output stream.

    6. Selected the new output stream. On the Properties view's General tab, added Name: MergedFeeds

Back to Top ^