Lock and Unlock Operators Sample

This topic explains how we built the Lock and Unlock operators sample.

This sample uses paired Lock and Unlock operators to protect shared data against concurrent writes. The sample is intended only to demonstrate how to protect the data, and does not include other concurrent operations which might access the data. Other concurrent operations could derive from data on different streams sharing the query table, or they could be generated by tuples on the same stream, which would have to wait for the tuple being protected by the Lock clearing the Unlock (presumably there would be additional operators between the Lock and Unlock to process the data.)

This example uses the same key for both the key into the Query Table and the key on which to lock for exclusive access.

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.

Installed Sample File Locations

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

How we Created the Lock and Unlock Sample

This sample uses paired Lock and Unlock operators to protect shared data against concurrent writes. The sample is intended only to demonstrate how to protect the data, and does not include other concurrent operations which might access the data. Other concurrent operations could derive from data on different streams sharing the Query Table, or they could be generated by tuples on the same stream, which would have to wait for the tuple being protected by the Lock clearing the Unlock.

Note: Normally there would be additional operators between the Lock and Unlock operators to process the protected data. This sample is kept deliberately simple to demonstrate the basic features.

  1. Launched StreamBase Studio

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

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

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

  5. Clicked it on the canvas, which invoked the Input Stream Properties view.

  6. On the General tab, Name WriteProtectedData

  7. On the Edit Schema tab, added:

    • Field Name: protectedDataKey, Type: int

    • Field Name: protectedDataValueInit, Type: string, Size: 8

  8. Created a Query Table:

    • Dragged a Query Table from the palette to the canvas.

    • Clicked it on the canvas, which invoked the Query Table Properties view.

    • On the General tab, Name: SharedDataQueryTable

    • On the Table Settings tab, Type: Memory

    • On the Edit Schema tab, added:

      • Field Name: sharedDataLockKey, Type: int

      • Field Name: protectedDataValue, Type: string, Size: 8

    • On the Primary Index tab:

      • Select sharedDataLockKey field and click the ">" icon to move this field into the Selected Fields window.

    • On the Secondary Index tab:

      • We did not use a secondary index in this sample.

  9. Created a Lock Set:

    • Dragged a Lock Set from the palette to the canvas.

    • Clicked it on the canvas, which invoked the Lock Set Properties view.

    • On the General tab, Name: SharedDataLock

    • On the Edit Schema tab, added:

      • Field Name: sharedDataLockKey, Type: int

  10. Created a Lock operator:

    • Dragged a Lock operator from the palette to the canvas.

    • Connected the WriteProtectedData input stream to the Lock operator.

    • Connected the SharedDataLock Lock Set to the Lock operator.

    • Clicked on Lock operator.

    • On the General tab, Name: LockSharedData

    • On the Lock Settings tab:

      • Expression: protectedDataKey

  11. Created a Query operator for write operations:

    • Dragged a Query operator from the palette to the canvas.

    • Connected the SharedDataQueryTable data construct to the Query operator (this will help fill in some symbol names).

    • Connected the LockSharedData output to the Query operator

    • Clicked on Query operator.

    • On the General tab, Name: WriteSharedData

    • On the Query Settings tab:

      • Operation: Write

      • Where: Primary Index

      • Matches:

        • Field Name: sharedDataLockKey (the value comes from the Query Table)

        • Expression: protectedDataKey

    • On the Operation Settings tab:

      • Type of Write: insert

      • Values to insert:

        • Expression: protectedDataValueInit

        • If insert fails because no row was found: Update existing row using value above

    • On the Output Settings tab:

      • Fields Available from Input Stream: unchecked both boxes

      • Fields Available from Query Table:

        • checked both boxes

        • Rename the outputs to sharedDataLockKey and protectedDataValue, respectively.

        We indicated that we want this Query operator to emit both query table fields as output (to the next downstream operator, the UnlockSharedData operator). Notice the new.<field-name> convention for the new values of these fields (as compared to the way the input fields are qualified: input.<field-name>).

  12. Created an Unlock operator:

    • Dragged an Unlock operator from the palette to the canvas.

    • Connected the SharedDataLock data construct to the Unlock operator.

    • Connected the WriteSharedData Query operator to the Unlock operator.

    • Clicked on Unlock operator.

    • On the General tab, Name: UnlockSharedData

    • On the Unlock Settings tab:

      • Expression: sharedDataLockKey

  13. Created an output stream:

    • Dragged an output stream from the palette to the canvas.

    • Clicked on the output stream

    • On the General tab, Name: ReturnProtectedData

    • Connected the UnlockSharedData Unlock operator to the ReturnProtectedData output stream.

Running the Lock and Unlock Sample

Running LockUnlock.sbapp in StreamBase Studio

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

  2. Click the Run StreamBase Application (Default Launch) button. (Not the Run button.) This opens the Test/Debug perspective and starts the application.

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

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

  5. Enter 1 and xyz in the protectedDataKey and protectedDataValueInit fields, respectively.

  6. Click Send Data, and observe this data appears in the Application Output view:

        sharedDataLockKey=1, protectedDataLockKey=xyz
    
  7. Click Run > Stop StreamBase Application (F9).

Running LockUnlock.sbapp in Terminal Windows

Open three terminal windows. In all windows, first change your working directory to where the samples are installed.

  1. In window 1, type:

    sbd LockUnlock.sbapp

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

  2. In window 2, type:

    sbc dequeue

    There is no output immediately, but the dequeuer is primed to display subsequent output of the application.

  3. In window 3, type:

    sbc enqueue WriteProtectedData

    The sbc command is now awaiting keyboard input. Then type: 1,xyz. Type Ctrl-Z (Windows) or Ctrl-D (UNIX), and sbc will exit.

    Observe these lines in the dequeue window: ReturnProtectedData,1,xyz.

  4. In window 3, type:

    sbadmin shutdown

    The sbadmin shutdown command will terminate the daemon and dequeuer.

Back to Top ^