CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol –...
Transcript of CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol –...
![Page 1: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/1.jpg)
Department of Computer Science
CS505: Distributed Systems
Lecture 12: Separation of Concerns
![Page 2: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/2.jpg)
Outline
Separation of concerns and aspect-oriented programming
AspectJ intro
![Page 3: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/3.jpg)
Application Logic vs Distribution
Distribution is hard to deal with
Intuition– Functional behavior, i.e., application logic, is main aspect of
program– Interaction behavior is another one
Hypothesis: distribution is orthogonal to application logic
– E.g., 2-tier / 3-tier architecture, reliable or unreliable communication, is “implementation detail”
![Page 4: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/4.jpg)
Meta-Object Protocols
Popular model– Every object is assigned a meta-object -- a “dual”
Assignment can be made lazily Meta-objects can be “chained”
– Meta-object controls everything that “goes in and out of object”– Cf. Behavioral/computational reflection [Maes’87]
Implementations– Early ones with proxies
“Self problem”, “encapsulation problem”– Deeper integration (runtime/compiler)
![Page 5: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/5.jpg)
Aspect-Oriented Programming
Reflective approach to concurrency– cf. [Briot et al.‘98]
With well-defined interfaces– More than only pre-/post-invocation actions
And an underlying methodology
![Page 6: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/6.jpg)
AOP Model
By generalizing, every program involves various aspects, e.g.
– Synchronization/concurrency– Persistence– Security– Replication– Logging– Debugging– …
These “cut across” application: crosscutting
![Page 7: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/7.jpg)
Crosscutting
Goal– Aspects can be dealt with by programmer in isolation each– Can be “plugged” into application
Aspects are rarely orthogonal– Developer must identify conflicts
Crosscutting concerns “Aspects are well-modularized crosscutting concerns”
– Aspects are weaved
![Page 8: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/8.jpg)
Variations
Static AOP– Similar to compile-time meta-object protocol– Aspects are defined like classes, types, etc.– Weaving occurs at compilation
Dynamic AOP– New aspects can be added on the go– Weaving occurs at runtime– Challenge: consistency at transition, “predict” weaving points
without high overhead
![Page 9: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/9.jpg)
AspectJ
Freely available at http://eclipse.org/aspectj
Concepts– Join points
Elementary points in main code for (inter)action with aspects– Pointcuts
Describes what triggers an aspect– Advice
Description of what to perform for pointcuts and when– Inter-type declarations
Additions to classes (class hierarchies)– Aspects
Define what pointcuts and advices apply to
![Page 10: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/10.jpg)
Highlevel View of AspectJ
Java Program
AspectJ
Advice
pointcut advice body
join point
![Page 11: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/11.jpg)
Join Points
Transition points or boundaries– Method call– Class boundary– Method execution– Access or modification of member variable– Exception handlers– Static and dynamic initialization
Examples– Method invocations, e.g., void Hello.print()– Field accesses, e.g., String Hello.message
Not declared individually in AspectJ
![Page 12: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/12.jpg)
Graphical View of Join Points
foo(){x = 0;if (x < 0) throw ...;}
barint x;
bar1.foo();
![Page 13: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/13.jpg)
Specifying Join Points with Designatorscall(method signature)handler(exception name)cflow(joinpoint designator): including call itselfcflowbelow(joinpoint designator): not including callthis(type name): callertarget(type name): call-site (type)within(class name): call-site (lexical)execution(method signature)get(field signature), set(field signature)initialization(signature), staticinitialization(type name)
![Page 14: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/14.jpg)
Designators with Wildcardscall(* foo())
call(public bar.*(..))
call(void foo(..))
call(* *(..))
call(*.new(int, int))
handler(File*Exception)
![Page 15: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/15.jpg)
Pointcuts
Set of join pointspointcut name(args): pointcut_designators;
Treats pointcut designators as predicatesSupport for pattern matching, e.g.,
– *– ||– &&
– !
Nesting with cflow(<pointcut>)Allows parameter passing from pointcut to the advice
– this, target, and args are used to “publish” attributes of pointcut
![Page 16: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/16.jpg)
Examples
pointcut MemberRead() : call(* *get*(..)) || call(* *Get*(..));
pointcut MemberReadOrWrite(): MemberRead() || call(* *set*(..)) || call(* *Set*(..));
pointcut A1(int t2, String m1): !target(t2) && (call(* c*.foo*(..)) || * get(m1));
![Page 17: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/17.jpg)
Advice
Pointcuts don’t do anything– Advice associate pointcuts with actions, and how to perform them
Advice forms– before(param) : pointcut(param) {body}– after(param) : pointcut(param) {body}– after(param) returning []: pointcut(param) {body}– after(param) throwing []: pointcut(param) {body}– type around(param) [throws typelist] : pointcut(param) {body}
– [ ] stands for <type object>
Parameter passing permitted from pointcuts to advice with– args(): arguments of call – this(): executing object (call-site)– target(): called object (callee)
![Page 18: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/18.jpg)
Examples
before() : MemberRead() {...}
after() : MemberReadOrWrite() {..}
before(): MemberRead() && within(Lock) {..}
![Page 19: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/19.jpg)
Parameter Information Flow
Join PointDesignator
Pointcut
Advice AdviceBody
![Page 20: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/20.jpg)
Example
pointcut MemberRead(int index) : args(index) && (call(*get*(..)) || call(*Get*(..)));
before(int index) : MemberRead(index) { System.out.println(index);}
![Page 21: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/21.jpg)
Inter-type Declarations / Introduction
Static changes/additions to classes– Reflecting logic handled by aspects
Forms – Add fields to a class– Add methods to a class– Inheritance and interface specification
![Page 22: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/22.jpg)
Examples
public int foo.bar(int x);
private int foo.counter;
declare parents: mammal extends animal;
declare parents: MyThread implements MyThreadInterface;
![Page 23: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/23.jpg)
Compiler Errors and Warnings
declare error: pointcut : message;– Outputs error at compilation and aborts
declare warning: pointcut : message;– Outputs warning at compilation and aborts
Note– Pointcut must be statically determinable, i.e., no cflow etc.
![Page 24: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/24.jpg)
Aspects
Aspects wrap up pointcuts, advice, and inter-type declarations
– Represent unit of “concern”
Declared similarly to classes– Can have local fields, methods– Can be declared abstract
![Page 25: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/25.jpg)
Ordering and Association
declare precedence : aspect*; – Provides ordering of application
Inheritance among aspects– Aspects can extend (inherit from) aspects
Thus possible to write subaspects e.g. for subclasses– Derived aspect takes precedence
By default one aspect instance– Hence the need for inter-type declarations?– Can be changed
pertarget(pointcut) perthis(pointcut)
![Page 26: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/26.jpg)
Reflection in AspectJ
thisJoinPoint– Similar to this in Java
Access point to introspection– JoinPoint interface– Able to find various information about the join point that triggered
the advice
![Page 27: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/27.jpg)
Example
aspect TraceNonStaticMethods {
before(Point p): target(p) && call(* *(..)) { System.out.println("Entering " + thisJoinPoint + " in " + p); } }
thisJoinPoint.toString()thisJoinPoint.getArgs()
![Page 28: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/28.jpg)
Example: Readers/Writersaspect RegistryReaderWriterSynchronizing of pertarget(readers() || writers()) {
// internal variables protected int activeReaders, activeWriters, waitingReaders,
waitingWriters;
// procedures protected synchronized void beforeRead() { ++waitingReaders; while (!(waitingWriters == 0 && activeWriters == 0)) { try { wait(); } catch (InterruptedException ex) {} } --waitingReaders; ++activeReaders; } protected synchronized void afterRead() {…} protected synchronized void beforeWrite() {…} protected synchronized void afterWrite() {…}
…
![Page 29: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/29.jpg)
Example (cont’d)
…
// pointcuts pointcut readers(): calls(Vector Registry.elementsNear(int, int)); pointcut writers(): calls(void Registry.add(FigureElement)) || calls(void Registry.remove(FigureElement));
// advices before(): readers() { beforeRead(); } after(): readers() { afterRead(); } before(): writers() { beforeWrite(); } after(): writers() { afterWrite(); }}
![Page 30: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/30.jpg)
Evaluation
In practice?– Often more complex scenarios than in toy examples– Inheritance issues to be dealt with in aspects rather than in main
code– Distribution (failures) are hard to handle automatically
[Kienzle&Guerraoui’02]– …
Distributed aspects– Several proposals, e.g. AWED, DADO, DJCutter [Nishizawa 2004]– Usually add remote joinpoints
Logical and physical location
![Page 31: CS505: Distributed Systems€¦ · Static AOP – Similar to compile-time meta-object protocol – Aspects are defined like classes, types, etc. – Weaving occurs at compilation](https://reader035.fdocuments.us/reader035/viewer/2022071219/605645adad893265e54ed5e9/html5/thumbnails/31.jpg)
ReferencesConcepts and Experiments in Computational Reflection. P. Maes.
OOPSLA 1988, 147-155.The Art of the Metaobject Protocol. G. Kiczales, J. des Rivières and
D.G. Bobrow. MIT Press, 1991.An Overview of AspectJ. G. Kiczales, E. Hilsdale, J. Hugunin, M.
Kersten, J. Palm, and W.G. Griswold. ECOOP 2001, 327-353. http://www.eclipse.org/aspectj/doc/released/progguide/starting-aspectj.htmlAOP: Does it Make Sense? The Case of Concurrency and Failures. J.
Kienzle and R. Guerraoui, ECOOP 2002, 37-61.Remote Pointcut: A Language Construct for Distributed AOP. N.
Nishizawa. AOSD 2004, 7-15.