High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt...

57
High Performance REST Applications © 2017 Sven Hasselbach

Transcript of High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt...

Page 1: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST Applications

© 2017 Sven Hasselbach

Page 2: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsÜber mich

Sven Hasselbach

Freiberuflicher IT Consultant seit 2003 IBM Champion 2013 Blog: http://blog.hasselba.ch Komme nicht aus der Schweiz

Page 3: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsÜbersicht

REST in aller Munde Möglichkeiten für REST Services auf Domino Servlets (Oldschool & als OSGi Plugins) Caching Multithreading REST JEE Applikations Framework Apache Wink, Jackson

Page 4: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST in aller Munde

REST ist defacto Web-Standard REST Schnittstellen lassen sich durch beliebige

Endsysteme (Geräte) aufrufen Trennung Frontend / Backend Aktueller Trend: Datenhaltung Domino /

Frontend „was anderes“

Page 5: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST Szenarien

REST Szenarien

Page 6: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsSzenario 1: Domino only

Domino Web Client

Frontend & REST Daten

Page 7: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsSzenario 2: Domino & anderer Webserver

Domino

Web Client

Node.jsSPA

Frontend

REST Daten

Page 8: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsSzenario 3: Domino als geschütztes Backend

Domino Web ClientNode.js

SPA

Frontend &REST Daten

RESTDaten

CRUD Operationen

z.B. Daten der kompletten View

Web App Operationen

z.B. Pagen über Daten der View

Page 9: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST auf Domino

REST auf Domino

Page 10: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Möglichkeiten (1)

Innerhalb NSF

Agents XAgents ExtLib REST Service Servlet (via Factory / DesignerServlet) DAS View (?ReadViewEntries) View (Eigener JSON Output)

Page 11: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Möglichkeiten (2)

Global

Oldschool Servlets Equinox Servlet (OSGi Plugin) JEE Applikation (OSGi Plugin)

Page 12: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Globale Servlets - Pro

Pro

Standard in Entwicklung, maximale Freiheiten Servlet Container läuft auf anderen Systemen Völlig lösgelöst von NSF

http://example.com/myServlet/ Global gültig, aus jeder NSF erreichbar

Keine Xpages-Runtime*http://example.com/db.nsf/myServlet/

*: je nach Art des Servlets

Page 13: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Globale Servlets - Contra

Contra

Zugriffsrechte greifen nicht (keine NSF) Bei programmatischem Zugriff auf eine NSF greift

ACL! Dokumentation?! Deployment erfordert HTTP Neustart (für alte

und neue Servlet-Arten)

Page 14: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsServlets & Domino-Umgebung

Servlets & Domino-Umgebung

Page 15: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Zugriff auf Domino

In alten Servlets: Per NotesFactoryNotesFactory.createSession()

In OSGi Servlets: Per ContextInfo Objektcom.ibm.domino.osgi.core.context.ContextInfo

Page 16: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Zugriff auf Domino – OSGi (1)

ContextInfo.getUserSession()Aktuell angemeldeter User

ContextInfo.getServerSession()Server Session

ContextInfo.isAnonymous()

Page 17: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Zugriff auf Domino – OSGI (2)

ContextInfo.getUserDatabase()● Aktuelle NSF, wenn innerhalb NSF

http://example.com/db.nsf/myServlet/ null, wenn außerhalb

http://example.com/myServlet/

Page 18: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsCaching

Caching

Page 19: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Arten des Cachens

Komplettes Ergebnis: REST Spezifikation erlaubtCaching von GET Abfragen

Teile der Daten cachen für REST Schnittstelle Lookup auf NAB Konfigurationsdaten View-Daten usw...

Page 20: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching

Je häufiger ein Ergebnis verwendet wird, destomehr Performance-Gewinn für das Ergebnis

Beispiel: 100 Anfragen pro Sekunde, Cachedauer 5

Sekunden, je eine Abfrage ins NAB → 499 Anfragen weniger auf Domino-Backend

Page 21: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching

Goldene Performance Regel: Alles, was gecacht werden kann, muss gecachtwerden

Caches haben „Gültigkeitsproblem“: Ist gecachter Wert noch aktuell? Wann ist Wert erneut zu cachen?

Page 22: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching

Was ist „Cachewürdig“? Für REST Schnittstelle ist nicht vorhersagbar,

welche Methode wie häufig aufgerufen wird Inkonsistenzen!

Für jede Methode / jeden Wert muss gutüberlegt sein, wie lange gecached wird Problem: Dokument wurde geändert. View? Ggf. muss getraced werden, wieviel Zeit ein

Code-Block benötigt

Page 23: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Memcached (1)

Memcached ist Cache-Server Läuft unabhängig von Domino, überlebt Neustart

des HTTP Tasks / Domino Servers Speichert alle Objekte, die übergeben werden

Key / Value Speichert im RAM Abrufe per Netzwerkabfrage

Page 24: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Memcached (2)

Pro

Beliebig skalierbar – unbegrenzt RAM &unbegrenzte Instanzen (Server) möglich

In Domino‘s JVM wird kein Speicher belegt Von überall erreichbar: XPages, Servlet, DOTS,

Java Agent, fremde Systeme Cache unterliegt zeitlichem Verfall – kein

„Aufräumen“ nötig

Page 25: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Memcached (3)

Contra

Nur serializierbare Objekte ablegbar Kein Zugriffsschutz auf memcached

Keine ACL Keine Authentifizierung: Jeder memcached

Client kann lesen / ändern Zugriff per Netzwerk – daher langsamer als

„interner“ Speicher ( JVM)

Page 26: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Memcached (4)

Fazit

Nur serializierbare Objekte ablegbar => Ideal für JSON!

Kein Zugriffsschutz auf memcached Keine ACL Keine Authentifizierung: Jeder memcached

Client kann lesen / ändern => Firewall / nur lokal auf Domino-Server

Page 27: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Memcached (5)

memcached erlaubt in Standardeinstellung nur 1MB Große Objekte, max 64 MB RAM

Lässt sich beliebig vergrößern Dank mehrfacher Instanzen auch auf mehreren

Servern

Page 28: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – spymemcached

Thirdparty Library für memcached Protokollspymemcached

Auch in JavaAgents einsetzbar

Page 29: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Caching – Demo

Demo

Memcached-Instanz: Code Beispiel-Implementierung ValueHolder – Methodik

Page 30: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsMultithreading

Multithreading

Page 31: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Mulithreading – Wozu?

Parallelisierung von Abfragen

UseCases Suche über mehrere Dbs Blockende Abfragen gleichzeitig verarbeiten Logging

Page 32: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Mulithreading – Wozu?

Hintergrundprozesse mit „Progress“-Indikator Report-Generierung Langläufige Prozesse

Hintergrundprozesse ohne Ergebnis Ergebnisse, die „nicht interessieren“, blocken

nicht länger Löschen von Dateien, Anlegen von Usern usw. Aufbereiten von Pager-Ergebnissen

Page 33: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Mulithreading – Besonderheiten

Domino Java Objekte sind eine Herausforderung Java-Threads haben ein unkontrollierbares

„Eigenleben“ Keine Unterstützung von Domino („Alles muss

man alleine machen“)

Page 34: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (1)

Domino Java Objekte sind mit C-Objekt„verbunden“

Domino View (Java)

Domino View (C Handle)

View view = db.getView(„All“)

Page 35: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (2)

Domino Java Objekte „nullen“ zerstört nicht denHandle

Domino View (Java)

Domino View (C Handle)

View view = db.getView(„All“);view = null;

Page 36: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (3)

C-Handle muss eigenständig zerstört werden

Domino View (Java)

Domino View (C Handle)

View view = db.getView(„All“);view.recycle();view = null;

Page 37: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (4)

Mehrere gleiche Domino Java Objekte teilen sichC-Handle

Domino View (Java)

Domino View (C Handle)

View view = db.getView(„All“);View view2 = db.getView(„All“);

Domino View (Java)

Page 38: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (5)

Zerstören eines Handles wirkt sich auf alleInstanzen aus

Domino View (Java)

Domino View (C Handle)

View view = db.getView(„All“);View view2 = db.getView(„All“);

view2.recycle()

Domino View (Java)

Page 39: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (6)

C-Handles sind wie Einkaufswagen

View view = db.getView(„All“);

Domino View (Java)

Page 40: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (7)

„Recycle“ bringt Wagen zurück

View view = db.getView(„All“);view.recycle()

Domino View (Java)

Page 41: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (8)

„Out of C-Handles“

View view = db.getView(„All“);

Domino View (Java)

?

Page 42: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (10)

Domino Java Objekte & Threads

View view = ...

Thread A

View view = ...

Thread B

?

Page 43: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (9)

Domino Java Objekte & Threads Von Session abgeleitete Objekte sind von

anderen Threads zugreifbar Wegen Synchronisation problematisch Daher jeder Thread eigene Objekte

Page 44: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Domino & Java (11)

Domino Java Objekte & Threads C-Handle verhindert Serializierung Jeder Thread muss Domino-Objekte selbst

erstellen Ohne Java Reflections kein „isRecycled“ Notes.sInitThread & NotesThread.sTermThread

machen Thread zu NotesThread (DLL!)

Page 45: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

ExecutorService verwaltet Threadpool ThreadFactory erstellt NotesThreads Runnable: Code wird ohne Rückgabewert

ausgeführt Callable/Futures: Code wird mit Rückgabewert

ausgeführt Queue & QueuePolicy: Was, wenn der Pool voll

ist?

Page 46: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

ExecutorService

ThreadPoolExecutor verwaltet Threads Anzahl der min. Threads Anzahl der max. Threads Lebenszeit eines Threads Factory für Threads Bestimmt Verwaltung des Queues Stellt Methoden für Verarbeitung bereit

Page 47: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

ThreadFactory

erstellt NotesWorkerThread wenn benötigt

Page 48: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

NotesWorkerThread

Erweiterung von NotesThread Initialisiert mit NotesThread.sinitThread() NotesDLLs Zerstört diese mit NotesThread.stermThread()

Page 49: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

Code

Page 50: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: Multithreading - Grundlagen

JEE Beispiel Applikation

● Apache Wink● Jackson

https://github.com/hasselbach/domino-rest-servlet

Page 51: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: REST JEE Applications - OSGI

Entwicklungsumgebung

Eclipse XPages SDK (von OpenNTF)

Page 52: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: REST JEE Applications - OSGI

Aufbau OSGI Plugin

Plugin Feature UpdateSite

Page 53: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: REST JEE Applications - OSGI

Plugin

plugin.xml Details über das Plugin Erweiterungspunkte und deren Definition „Bauanleitung“ für den Build

Activator Initialisiert Plugin

Page 54: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: REST JEE Applications - OSGI

Plugin

WebContent web.xml (Definition der Applikation) Libraries, statische Seiten, ..

Definition in Extension

Page 55: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsREST & Domino: REST JEE Applications - OSGI

JEE Applikation

RestApiApplication● Initialisiert Apache Wink & Jackson● Definiert „Verarbeitungsklasse“

Page 56: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsCode Deepdive

Code!

Page 57: High Performance REST Applications€¦ · In OSGi Servlets: Per ContextInfo Objekt com.ibm.domino.osgi.core.context.ContextInfo. High Performance REST Applications REST & Domino:

High Performance REST ApplicationsDanke!

Vielen Dank!