The SQL-NS Application Model


SQL-NS can be used to build a variety of notification applications with different uses and for different application domains. But, when viewed from the highest level, all these notification applications conform to the same basic model, shown in Figure 3.1.

Figure 3.1. High-level view of a notification application.


Data enters the application from the outside world. This data can be pulled in by the application or pushed in by an external source. In SQL-NS terms, each piece of data is referred to as an event because it represents some happening in the outside world that may potentially be of interest to some subscribers. An event may be a new price for a stock, a notice of a new concert listing, or a gate change for a flight.

The notification application maintains users' subscriptions. Subscriptions are users' declarations of what kinds of events interest them. When events arrive, the application matches them against the subscriptions and produces a set of notifications. These notifications are delivered to the end users.

Events as Data

Events are descriptions of things that happen in the real-world that can be represented as data. For example, a change in the price of a stock (an event potentially of interest to a stockbroker client) can be described as a piece of structured data containing a stock symbol field and a stock price field. A traffic incident event (for an application that notifies commuters about road conditions) might contain a field that describes the location of the incident and another describing the incident type (accident, road closure, weather warning, and so on).

Whatever the type of event, its description can be modeled as data. The structure of the data can be described with a schema that indicates the names of the fields and their data types. Given this schema, it's easy to construct a database table to store the event data. For example, stock events can be stored as rows in a table, as shown in Figure 3.2.

Figure 3.2. Modeling stock events as rows in a table.

Stock Symbol

Stock Price

XYZ

55.55

PQS

95.30

JKL

15.00



Subscriptions as Data

Thinking of events as data is usually quite natural. Although it may be somewhat less intuitive at first, subscriptions can be modeled as data, too. Think of the subscriptions in a stock notification application. Let's say that all subscriptions will have the form

"Notify me when the price of stock S reaches price target T."

where S represents some stock symbol and T represents a price target. A subscription example might be

"Notify me when the price of stock XYZ reaches price target $50.00."

If all the subscriptions are constrained to this form, an individual subscription can be represented as a pair of values for S and T. Such subscriptions could be stored in a table, as shown in Figure 3.3.

Figure 3.3. Modeling stock subscriptions as rows in a table.

Subscriber

Stock Symbol (S)

Price Target(T)

Bob

XYZ

50.00

Alex

PQS

90.00

Mary

JKL

12.00

Jane

PQS

100.00



Each row identifies the subscriber, the stock symbol, and the price target of interest. For illustrative purposes, the subscriber is represented by a name, but in an actual application, a more appropriate identifier may be used.

Matching Events with Subscriptions

As mentioned in Chapter 1, "An Overview of Notification Applications," the matching of events against subscriptions is the key function of any notification application. If the matching can be implemented efficiently, the application will scale to large volumes.

With events and subscriptions both represented as data, matching can be accomplished by means of a SQL join. Given the table structures in Figures 3.2 and 3.3 for events and subscriptionsand let's say that we called the events table Events and the subscriptions table Subscriptionsthe following SQL statement would determine the matches:

 SELECT  S.Subscriber, E.StockSymbol, E.StockPrice FROM    Events E JOIN Subscriptions S ON      E.StockSymbol = S.StockSymbol WHERE   E.StockPrice >= S.PriceTarget 


This statement joins the stock events table with the subscriptions table on stock symbol and then selects rows where the stock price in the event is greater than or equal to the stock price target specified in the subscription. The rows returned by this query represent the set of matches between the events and subscriptions that should result in notifications being sent. For the particular data shown in the preceding examples, Figure 3.4 shows the results of the query.

Figure 3.4. Results of matching events with subscriptions.

Subscriber

Stock Symbol

Stock Price

Bob

XYZ

55.55

Alex

PQS

95.30

Mary

JKL

15.00



Note that only three of the four subscriptions matched: Jane's subscription specified a price target for PQS of 100.00, and because the event for PQS indicated that the price was only 95.30, the matching query did not return a row for Jane.

Each of the rows in the results table is the raw data for a notification to be sent. Thus, notifications also can be modeled as rows of data in a table. The notification data can later be packaged into a readable message and delivered to the appropriate subscriber.

Scalability of the SQL-NS Application Model

The modeling of both events and subscriptions as data is a key innovation of SQL-NS and the basis for its inherent scalability. Because both events and subscriptions are rows in tables, SQL joins can be used to match them. In general, SQL joins are extremely efficient at matching large sets of data; more than 20 years of query processing and indexing developments make this possible. As long as a reasonable join query can be written for a particular event and subscription schema (in most cases, one can), the cost of matching (in terms of computing resources) will be low. Furthermore, this cost grows sublinearly with the amount of data. That is, if you double the number of events or subscriptions, the cost of matching increases by less than a factor of two.

This model is different from that used by most other pub-sub systems. Most other systems model individual subscriptions as queries, rather than data. The simplest of these systems evaluates the subscription queries one-by-one for a given set of events. This strategy is expensive, and as the number of subscriptions and events grows, the cost of evaluation growsat best, linearly, at worst, exponentially. The more sophisticated of these systems attempt to obtain performance gains by indexing the subscription queries and looking for logical shortcuts in the query evaluation. But the effectiveness of these strategies is limited by the structural differences and complexity of the subscriptions and isn't always assured. In many cases, systems that model subscriptions as queries can do little better than one-at-a-time evaluation.

Note

To be absolutely clear, the SQL-NS application model does use queries to evaluate subscriptions. But it does not model each individual subscription as a query. Instead, in the SQL-NS application model, there is one query for each subscription type. This query evaluates all subscriptions of that type at once. This is the key differentiator between SQL-NS and other pub-sub systems.


When you build a SQL-NS application, you have a choice of two models for subscription evaluation. In the first model, developer-defined logic is used to determine whether a match between an event and a subscription has occurred. In this model, you, the application developer, write the SQL join query that finds the matches. This query might look something like the SQL statement shown earlier in the "Matching Events with Subscriptions" section (p. 43).

By writing the query, you are defining the conditions that must be true for a match to occur. The subscribers can specify only values for the data referenced in the query. For example, the logic coded into the stock application's query says that a stock event matches a stock subscription when the stock symbols are the same and the stock price in the event is greater than, or equal to, the stock price target in the subscription. Subscribers cannot change this logic; they can only provide values for the stock symbol and stock price target in individual subscriptions.

In the second subscription evaluation model, user-defined logic determines whether an event matches a subscription. The users of the application (the subscribers) choose the conditions that must be true for a match to occur. These conditions can be arbitrary combinations of Boolean predicates based on the event data. In this model, each subscription can specify a different matching condition. For example, given the stock event data, one subscription's condition might state that a match occurs when the stock price in the event is less than a particular price target. Another subscription's condition may stipulate that a match occurs when the stock price in the event is within a particular range of values.

Regardless of whether you choose developer-defined or user-defined logic for the subscriptions in your application, SQL-NS uses queries to evaluate groups of subscriptions at the same time. In the case of developer-defined logic, the query provided by the developer is associated with a named subscription type, and all subscriptions of that type are evaluated by the single query. When user-defined logic is used, SQL-NS also uses a single query to evaluate subscriptions of a common type. The difference is that subscriptions are grouped into types based on the logical structure of their conditions (rather than assigned to predefined, named subscription types), and the queries used to evaluate them are constructed dynamically by SQL-NS. For a description of how SQL-NS uses queries to evaluate subscriptions with user-defined logic, refer to the "Evaluating User-Defined Logic with Queries" sidebar (p. 45).

Evaluating User-Defined Logic with Queries

To evaluate user-defined logic, SQL-NS begins by translating each subscription into a SQL query that selects from the events table the rows that satisfy the subscription condition. The condition logic specified in the subscription is implemented in the WHERE clauses of this query. For example, suppose a subscriber, Bob, wants to be notified when the XYZ stock price falls between 45 and 75. He might enter a subscription with the following condition:

[View full width]

(E.StockSymbol = 'XYZ') AND ((E.StockPrice > 45) AND (E .StockPrice <= 75))


This could be translated into the following SQL query:

 SELECT  'Bob' AS Subscriber, E.StockSymbol, E.StockPrice FROM    Events E WHERE   E.StockSymbol = 'XYZ'         AND (E.StockPrice > 45 AND E.StockPrice <= 75) 


When two or more subscription conditions translate into SQL queries with the same structurethat is, queries with the same combinations of logical constructs that differ only in terms of constant data values, SQL-NS builds a single query to evaluate them. That query does not contain hard-coded data values (as the one in the preceding example does), but it references a parameters table that stores the specific constant values for each subscription. For example, suppose that in addition to Bob's subscription we just examined, another subscriber, Mary, wants to be notified when the PQS stock price falls between 80 and 120. Mary's subscription condition

[View full width]

(E.StockSymbol = 'PQS') AND ((E.StockPrice > 80) AND (E .StockPrice <= 120))


could be translated into the following query:

 SELECT  'Mary' AS Subscriber, E.StockSymbol, E.StockPrice FROM    Events E WHERE   E.StockSymbol = 'PQS'         AND (E.StockPrice > 80 AND E.StockPrice <= 120) 


But because this query is structurally the same as the query that evaluates Bob's subscription, both subscriptions could be evaluated together by the following single query:

[View full width]

SELECT P.Subscriber, E.StockSymbol, E.StockPrice FROM Events E, SubscriptionParameters P WHERE E.StockSymbol = P.Parameter1 AND (E.StockPrice > P.Parameter2 AND E.StockPrice <= P .Parameter3)


This query relies on the SubscriptionParameters table to provide the constant data values for each subscription: for Bob's subscription, the values are the stock symbol, XYZ, and the price targets, 45 and 75; for Mary's subscription, the stock symbol is PQS, and the price targets are 80 and 120. To evaluate additional subscriptions that have the same logical form, new rows are needed in the parameters table, but the same query can evaluate them all together.

Because the same fundamental condition can be expressed in many ways using Boolean constructs, SQL-NS attempts to normalize the condition statements before translating them into SQL queries. This maximizes the opportunity for the same query to be used for multiple subscriptions.

Note that the queries and table structures shown here are simplified for ease of explanation. In actual applications, the queries and supporting tables generated by SQL-NS are somewhat more complex because they are designed to accommodate an unlimited number of parameters per query and support many different data types. However, they are based on the basic principles described here.


Your choice of subscription-evaluation model (developer-defined or user-defined logic) affects the scalability of your applications. Generally, applications that use developer-defined logic are more scalable because subscription grouping is enforced by the developer. With user-defined logic, the extent to which grouping is possible depends on the similarity of the subscriptions entered. In the worst case, if every subscription condition has a different structure, no grouping is possible and the application would have to evaluate one subscription at a time. In practice, it's unlikely that this worst-case scenario would actually play out; in most cases, some degree of grouping is achievable.

Just because you have the option of supporting user-defined logic in your SQL-NS applications does not mean you always should. It's worth questioning whether the flexibility offered by user-defined logic, which requires a sacrifice in scalability, is really needed in your applications. The obvious drawbacks to using developer-defined logic are that all subscriptions must have the same structure, and the only kinds of subscriptions users can enter are those the developer has implemented. But do your users really need to be able to enter arbitrarily complex subscriptions? Does each user need to be able to enter a different kind of subscription? Or can you devise a set of subscription types that cover the overwhelming majority of subscriptions users will want to enter?

Remember that SQL-NS enables you to implement several types of subscriptions in a single application, each with a separate data schema and matching query. Experience has shown that in the vast majority of applications, developers can predict what subscriptions users will want to enter and implement those as predefined subscription types. Often, the lack of flexibility in the subscription-matching logic is not even noticed by the users of these applications.

All the sample applications in Parts I, II, and III of this book (including the sample application in this chapter) use only developer-defined matching logic. The use of user-defined matching logic is covered in Part IV, in Chapter 18, "User-Defined Matching Logic in SQL-NS Applications." At this point, you should focus on learning the fundamental SQL-NS concepts, the majority of which are the same, regardless of which subscription evaluation model you choose.

Programming to the SQL-NS Application Model

In essence, the SQL-NS application model views events and subscriptions as data and uses SQL joins to match them. As a developer building on the SQL-NS platform, you define the following aspects of your application:

  • Schemas for the event, subscription, and notification data

  • Logic that the application executes to perform matching and maintain state

  • Configuration of the SQL-NS execution engine components that run the application

You provide all this information in an XML document called an Application Definition File (ADF). Think of the ADF as the source code for the application: to create a new application, you author a new ADF.

Note

SQL-NS provides an XSD schema that defines the XML elements an ADF may contain. As you work through this and subsequent chapters of this book, you'll learn about these XML elements in detail. This chapter highlights specific pieces of the stock notification application's ADF.


When you write an ADF, you usually begin with the elements that define schemas. Each schema is a description of the size and shape of one kind of data. You provide the names of the fields and their data types, much as you would if you were defining a SQL table. The schema for the events specifies the structure of the data your application receives from its event sources. The schema for the subscription data describes the information each subscriber will provide when creating a subscription. The notification schema describes the data content of the notifications your application will deliver.

In addition to the data schemas, the ADF also specifies how events and subscriptions are matched to form notifications. If you choose developer-defined logic as your subscription evaluation model, the matching logic you provide in the ADF consists of actual SQL statements that find the matches. These statements operate on the event and subscription data (as defined by their respective schemas) and produce a row of data for each notification to be sent (the columns in the resultsets correspond to the fields in the notification schemas). If instead you choose user-defined logic as your evaluation model, in the logic sections of the ADF, you provide SQL statements that produce notification data (conforming to the notification schemas) from event and subscription data that has already been determined to match (according to the user-defined conditions).

In the component configuration section of the ADF, you specify how the SQL-NS engine components should run your application. These components perform such functions as gathering event data from event sources, coordinating the execution of the matching logic, and delivering notifications to their destinations. You can specify which components should be used in your application, how they are distributed across various servers, what resources they should use, and when they should run.

When your ADF is complete, you compile it using the SQL-NS compiler. The compiler translates the XML application definition into a set of database objects that will be used to run the application. These include tables for configuration settings, events, subscriptions, and notifications, as well as stored procedures that execute the SQL statements you provided in the ADF. The compiler installs these database objects into a database on your SQL server and populates some of the tables with the configuration information from the ADF. When you run your application, the SQL-NS engine connects to this database, reads the configuration information, and starts its various components; those components then interact with the database (reading and writing data) as they perform their execution functions. This process is illustrated in Figure 3.5.

Figure 3.5. The ADF is compiled into database structures, and the SQL-NS engine runs the application.


Note

Some important parts of a complete, running applicationsuch as the event sources, the subscription management interface, and the delivery systemsare not shown in Figure 3.5. Usually, these other parts of the application also interact with the application database, alongside the SQL-NS engine.


In the remainder of this chapter, we'll create and run the stock notification application. In doing so, we'll look at the data schemas, matching logic, and component configuration, as specified in the application's ADF. Although there are other aspects to the application, we'll focus on these for now because they form the application's core and are the most illustrative of the SQL-NS application model.

Note

To work through the steps in this chapter, you need to set up your development environment as described in Chapter 2, "Getting Set Up." If you have not already done so, go through the steps in that chapter before proceeding.





Microsoft SQL Server 2005 Notification Services
Microsoft SQL Server 2005 Notification Services
ISBN: 0672327791
EAN: 2147483647
Year: 2006
Pages: 166
Authors: Shyam Pather

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net