Sunteți pe pagina 1din 200

A Quick

Tour
of Logos

The Logical Appeal

So what exactly is logic? Who


cares?
Informally, logic is about saying things that make
sense. You can think of it in that way if you like.
It's pretty sunny today, so you should wear sunscreen.

Formally, logic is the art of arguing not like a fight


or debate, but by using the information we already
know to draw new and useful conclusions.

If it's sunny today, you should wear sunscreen.


Indeed it is sunny today.
Therefore, you should wear sunscreen.

But wait. That just looked like the exact


same thing you said before, you hack.

Well, yes. But that's how an argument looks in


form! You can break down any argument into
that makes it easier to think about.

If it's sunny today, you should wear sunscreen


Premises/
givens
Conclusion

It is sunny today.
___________________________________________
Therefore, you should wear sunscreen.

(Premises always come first, and the conclusion always co

How about a more complex


argument?
1. This piece of fresh fruit is fuzzy.
2. It also has seeds.
3. If a fruit is fuzzy, it's either a kiwi or a peach.
4. Peaches have a pit; they don't have seeds.
5. So the fruit can't be a peach.
6. So the fruit must be a kiwi.

Which of the above sentences is a conclusion?

How about a more complex


argument?
1. This piece of fresh fruit is fuzzy.
2. It also has seeds.
3. If a fruit is fuzzy, it's either a kiwi or a peach.
4. Peaches have a pit; they don't have seeds.
5. So the fruit can't be a peach.
6. So the fruit must be a kiwi.

Good logic lets us cobble together lots of


different pieces of information, and tell from
them what's probably or definitely true.

But what counts as good


logic?
That argument was good (made sense), because
the conclusion followed from the premises. We'll
see what this means in a moment.
Why don't we look at a bad argument?

But what counts as good


logic?
Some people have fallen off cliffs and lived.

Therefore, if I jump off this cliff, I will definitely be fin

Come on.
What could
possibly go
wrong?

But what counts as good


logic?
Some people have fallen off cliffs and lived.

Therefore, if I jump off this cliff, I will definitely be fin

This argument is weak. Although the premise i


it's easy to think of ways (very painful ways) th
conclusion could be false. The easiest way to s
logic is to do just that: try to think of another w
(Philosophers call these counterexamples).

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. So Philip
can breathe fire.
Is there a piece of the puzzle missing?

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. Philip is an
intern.
So Philip can breathe fire.

Sometimes you may encounter hidden state


and ideas, which the writer sneaks in but does
say outright.

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. Philip is
an intern.
So Philip can breathe
fire.
Are the premises true?
If so, then the conclusion's
100% guaranteed true.
No getting around it!

But what counts as good


logic?
Let's look at two kinds of arguments.
2. Inductive reasoning:
I touched a stove and it burned me. I did this
fifty times, and the same thing happened. So
the
next time I touch the stove, it will burn me.

Are the premises true?


If so, then the conclusion's
LIKE
probably true. There might
still be exceptions.

WHAT?

How is this useful to me?


Like Mr. Morgan said, the ability to make
strong logical arguments will become more
and more important later on in high school
and college.
Pathos and ethos are still valuable! But
your audience will be a lot better at
questioning them. Logos is handy because,
if you use it well, it can't really be
disproved.

How is this useful to me?


But even better is the superpower to spot
weak logic.

Next time you watch TV or go online (with


your parents' permission, of course), try to
keep track of how many different arguments
are being pitched to you by ads. How much
info is given to you? How much is left out?

What time is it?


Adven- wait, no. Activity time!

Pair off into groups of four. Each group will receive an ex


of a poor argument (these may be either inductive or de

With your group, you will have 5 minutes to try to come


one counterexample - one way in which the argumen
could be wrong, even if the premises are definitely true
Poke it full of holes!
Also, choose a group representative to tell us your reaso
(It's OK to imagine unlikely or weird explanations;
don't be afraid to think outside the box.
Oddly enough, logic has very little to do with facts.)

Some examples:

ARGUMENT: I pulled an all-nighter studying for last week


test, and I ended up with an A. Tiredness must make me

COUNTER: What if you got an A because you actually stu


Or maybe the test was going to be easy for you all along

ARGUMENT: If I play with Dad's power tools, he'll yell at


But Dad is yelling at me for something. So I guess I must
played with the power tools.

COUNTER: What if he's yelling at you for a different reaso


scratching the car, or hammer-throwing the cat onto the

John Keats

"To Autumn" is a poem by English


Romantic poet John Keats (31
October 1795 23 February 1821).

"To Autumn" is the final work in a


group of poems known as
Keats's "1819 odes".

"To Autumn" is a poem of three


stanzas, each of eleven lines. Written
in 1819, the structure is that of an
odal hymn, having three clearly
defined sections corresponding to the
Classical divisions of strophe,
antistrophe, and epode

The imagery is richly achieved


through the personification of Autumn

Poem

Season of mists and mellow fruitfulness,

Close bosom-friend of the maturing


sun;

Conspiring with him how to load and bless

With fruit the vines that round the thatch-eves


run;

To bend with apples the moss'd cottagetrees,

And fill all fruit with ripeness to the


core;

To swell the gourd, and plump the hazel


shells

With a sweet kernel; to set budding


more,

And still more, later flowers for the bees,

Until they think warm days will never cease,

For Summer has o'er-brimm'd their clammy


cells.

Who hath not seen thee oft amid thy store?

Sometimes whoever seeks abroad may


find

Thee sitting careless on a granary floor,

Thy hair soft-lifted by the winnowing


wind

Or on a half-reapd furrow sound


asleep,

Drowsed with the fume of poppies, while thy hook

Spares the next swath and all its twind


flowers:

And sometimes like a gleaner thou dost keep

Steady thy laden head across a


brook;

Or by a cyder-press, with patient look,

Thou watchest the last oozings, hours by


hours.

Where are the songs of Spring? Ay, where are


they?

Think not of them, thou hast thy music too,

While barrd clouds bloom the soft-dying day

And touch the stubble-plains with rosy


hue;

Then in a wailful choir the small gnats mourn

Among the river-sallows, borne


aloft

Or sinking as the light wind lives or


dies;
sinking

And full-grown lambs loud bleat from hilly bourn;

Hedge-crickets sing; and now with treble soft

The redbreast whistles from a gardencroft;

And gathering swallows twitter in the


skies.

Everything You Wanted to Know

About Writing Async,


Concurrent HTTP Apps
in Java

Agenda
Mostly this:

Agenda
And this:

Agenda
And this:

About your speakerh


c
u
g it
r
a
b
j
@
hu
b. c
om
/
linkd.in/jbaruchjba
ru
ch

stackoverflow.com/users/402053/

What Frog?

What Frog?

What Frog?

What Frog?

Requirements

parallel file Downloads


Parallel file parts
interrupt/pause/resume
Progress events
Checksums caching

First Association for concurrent


downloader

Lucky day: Download manager


written in java!

Lets look if we can use it!

1. No traceable license
2. No website or docs
3. No traceable sources
4. Its an app, not a lib

Java.net.urlconnection

1. Memory wasteful (buffering)


2. Minimal API
3. Blocking streams

What were looking for

1. Async/non-blocking
2. Event callbacks

What is IT going to take

1. Reactor
2. nio

Welcome to the reactor

pattern for lightweight


concurrency
Event driven
Threads reuse
Uses non-blocking Io

Original pattern

http://www.dre.vanderbilt.edu/~schmidt/PDF/reactorsiemens.pdf

Guess the author by the diagram

http://gee.cs.oswego.edu/dl/cpjslides

In Java,
Reactor means
NIO

Selector as a multiplexer

Java version - Registering


SocketChannelchannel=SocketChannel.open();
socketChannel.connect(new
InetSocketAddress("http://remote.com",
80));
...
Selectorselector=Selector.open();
channel.configureBlocking(false);
SelectionKeyk=channel.register(selector,
SelectionKey.OP_READ);
k.attach(handler);

Java version - Dispatcher


while(!Thread.interrupted()){
selector.select();
Setselected=selector.selectedKeys();
Iteratorit=selected.iterator();
while(it.hasNext())
SelectionKeyk=(SelectionKey)
(it.next();
((Runnable)(k.attachment())).run();
selected.clear();
}

Handling reactor events is


complex

Need to maintain state


Buffering assembling
chunks
Coordinating async events

Nio libraries

Most of them are servers


Netty, grizzly, etc.

Apache Mina
Apache HTTP components
asyncclient
Ning http client

Client and server nio library


Evolved from netty
Latest release October 2012

Nio libraries

Nings async http client

Here it is!

try (AsyncHttpClient asyncHttpClient = new AsyncHttpClient()) {


ListenableFuture<Response> future =
asyncHttpClient.prepareGet(
"http://oss.jfrog.org/api/system/ping").execute(
new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) {
System.out.println(response.getResponseBody());
return response;
}

@Override
public void onThrowable(Throwable t) {
t.printStackTrace();
}
});
Response response = future.get();

HAC Concepts

Request producer
Response consumer

try (CloseableHttpAsyncClient asyncHttpClient = HttpAsyncClients.createDefault()) {


asyncHttpClient.start();
Future<HttpResponse> future = asyncHttpClient.execute(
HttpAsyncMethods.createGet("http://oss.jfrog.org/api/system/ping"),
new AsyncByteConsumer<HttpResponse>() {
@Override
protected void onResponseReceived(final HttpResponse response) {
System.out.println(response.getStatusLine().getReasonPhrase());
}
@Override
protected void onByteReceived(final CharBuffer buf, final IOControl ioctrl) { }
@Override
protected void releaseResources() { }
@Override
protected HttpResponse buildResult(final HttpContext context) {
return (HttpResponse) context.getAttribute("http.response");
}

}, null);
HttpResponse response = future.get();

Choosing between ning and http


asyncclient

"All problems in computer


science can be solved by
another level of indirection"
David
Wheeler

public interface HttpProviderDownloadHandler {


void onResponseReceived(int statusCode, Map<String,
List<String>> headers);
boolean onBytesReceived(ByteBuffer buf);
void onFailed(Throwable error);
void onCanceled();
}

void onCompleted();

Head to head
Feature/Library Ning client
Maturity
Good
Download
Easy
cancelation

Http Async Client


Very new (early 2014)
With bugs

Progress
hooks

Just use

Events not
granular
enough

Documentation A bit sparse


Server-side
None, client
counterpart
only

onByteReceived()

Minimal
org.apache.httpcompon
ents

Performance?

http://blogs.atlassian.com/2013/07/http-client-performa

Rfc2616: a universe of its own

Confused?

Just read some stackoverflow


(and improve your rep as you go)

And that one


for
discovering
that range
header is lost
on redirect

Question!

What should
be contentlength when
using
compression?

https://github.com/http2/http2-spec/issues/46

Question!

Why when redirected to CDN


all the chunks start from zero?

HttpAsyncClientBuilder builder = HttpAsyncClients.custom();


// add redirect strategy that copies "range" headers, if exist
builder.setRedirectStrategy(new DefaultRedirectStrategy() {
@Override
public HttpUriRequest getRedirect(HttpRequest request, HttpResponse
response,
HttpContext context)
HttpUriRequest redirectRequest = super.getRedirect(request,
response, context);
// copy "Range" headers, if exist
Header[] rangeHeaders = request.getHeaders(HttpHeaders.RANGE);
if (rangeHeaders != null) {
for (Header header : rangeHeaders) {
redirectRequest.addHeader(header);
}
}
return redirectRequest;
}});

Question!

How many simultaneous


connections should I
open?

Question!

Whats
wrong with
the
following
code?

public static String


encodeUrl(String urlStr) {
URLEncoder.encode(urlStr,
"UTF-8");
...
}

Decoded URLs cannot be


re-encoded to the same form
http://example.com/?query=a&b==c
Cannot be decoded back after it
was encoded:
http
://example.com/?query=a%26b==c

Dont use java.net.URLEncoder


Utility class for HTML form
encoding. This class contains
static methods for converting a
String to the application/x
wwwformurlencodedMIME
format.
For more information about HTML
form encoding, consult the HTML
specification.

AHC Alternatives

Question!

How do I
close a
socket
correctly?

How hard can it be to close a socket?

The art of socket closing

http://www.safaribooksonline.com/library/view/http-the-

Half-closed: no new customers

Never block in socket close()

The other side expects


you to clean up nicely
It will give up on time out
You will wait (forever)

Remember?

Question!

How can I
write file parts
concurrently?

Write to separate files,


combine on finish
Write to same file, seeking to
the right position

OS Level File Locking


Advisory exclusive
private FileLock lock(FileChannel fileChannel) throws
IOException {
FileLock lock =
fileChannel.tryLock(Long.MAX_VALUE - 1, 1, false);
if (lock == null) {
throw new OverlappingFileLockException();
}
return lock;
}
WTF?!

VM Level File Locking

VM Level File Locking


Prevent same VM threads writing to
the file when we started closing it
Closing sequence:
Release file locks
Close channels
Rename a file to it's final name
(remove .part)
Erase progress info

VM Level File Locking


ReentrantReadWriteLock.ReadLock writeToFileLock =
rwl.readLock();
ReentrantReadWriteLock.WriteLock closeFileLock =
rwl.writeLock();
public void close() throws IOException {
this.closeFileLock.lock();
}
public int write(int partIndex, ByteBuffer buf) {
if (!this.writeToFileLock.tryLock()) {
throw new IllegalStateException("File is
being closed");
}
...
}

Whats next?

http/2
Mostly standardizing Google's
spdy

Header compression
multiplexing
Prioritization
Server push

On the way clear some stuff


E.g. compressed content length

Ease the load

Links!
RTFM: RFC 2616
Ultimate book: HTTP: The Definitive Guide
Amazon
Safari

Reactor pattern
Doug Lea on NIO

No, Thank you!

A Quick Tour
of Logos

The Logical Appeal

So what exactly is logic? Who


cares?
Informally, logic is about saying things that make
sense. You can think of it in that way if you like.
It's pretty sunny today, so you should wear sunscreen.

Formally, logic is the art of arguing not like a fight


or debate, but by using the information we already
know to draw new and useful conclusions.

If it's sunny today, you should wear sunscreen.


Indeed it is sunny today.
Therefore, you should wear sunscreen.

But wait. That just looked like the exact


same thing you said before, you hack.

Well, yes. But that's how an argument looks in


form! You can break down any argument into
that makes it easier to think about.

If it's sunny today, you should wear sunscreen


Premises/
givens
Conclusion

It is sunny today.
___________________________________________
Therefore, you should wear sunscreen.

(Premises always come first, and the conclusion always co

How about a more complex


argument?
1. This piece of fresh fruit is fuzzy.
2. It also has seeds.
3. If a fruit is fuzzy, it's either a kiwi or a peach.
4. Peaches have a pit; they don't have seeds.
5. So the fruit can't be a peach.
6. So the fruit must be a kiwi.

Which of the above sentences is a conclusion?

How about a more complex


argument?
1. This piece of fresh fruit is fuzzy.
2. It also has seeds.
3. If a fruit is fuzzy, it's either a kiwi or a peach.
4. Peaches have a pit; they don't have seeds.
5. So the fruit can't be a peach.
6. So the fruit must be a kiwi.

Good logic lets us cobble together lots of


different pieces of information, and tell from
them what's probably or definitely true.

But what counts as good


logic?
That argument was good (made sense),
because the conclusion followed from the
premises. We'll see what this means in a
moment.
Why don't we look at a bad argument?

But what counts as good


logic?
Some people have fallen off cliffs and lived.

Therefore, if I jump off this cliff, I will definitely be fin

Come on.
What could
possibly go
wrong?

But what counts as good


logic?
Some people have fallen off cliffs and lived.

Therefore, if I jump off this cliff, I will definitely be fin

This argument is weak. Although the premise i


it's easy to think of ways (very painful ways) th
conclusion could be false. The easiest way to s
logic is to do just that: try to think of another w
(Philosophers call these counterexamples).

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. So Philip
can breathe fire.
Is there a piece of the puzzle missing?

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. Philip is an
intern.
So Philip can breathe fire.

Sometimes you may encounter hidden state


and ideas, which the writer sneaks in but does
say outright.

But what counts as good


logic?
Let's look at two kinds of arguments.
1. Deductive reasoning:
All interns can breathe fire. Philip is
an intern.
So Philip can breathe
fire.
Are the premises true?
If so, then the conclusion's
100% guaranteed true.
No getting around it!

But what counts as good


logic?
Let's look at two kinds of arguments.
2. Inductive reasoning:
I touched a stove and it burned me. I did this
fifty times, and the same thing happened. So
the
next time I touch the stove, it will burn me.

Are the premises true?


If so, then the conclusion's
LIKE
probably true. There might
still be exceptions.

WHAT?

How is this useful to me?


Like Mr. Morgan said, the ability to make
strong logical arguments will become more
and more important later on in high school
and college.
Pathos and ethos are still valuable! But
your audience will be a lot better at
questioning them. Logos is handy because,
if you use it well, it can't really be
disproved.

How is this useful to me?


But even better is the superpower to spot
weak logic.

Next time you watch TV or go online (with


your parents' permission, of course), try to
keep track of how many different
arguments are being pitched to you by
ads. How much info is given to you? How
much is left out?

What time is it?


Adven- wait, no. Activity time!

Pair off into groups of four. Each group will receive an ex


of a poor argument (these may be either inductive or de

With your group, you will have 5 minutes to try to come


one counterexample - one way in which the argumen
could be wrong, even if the premises are definitely true
Poke it full of holes!
Also, choose a group representative to tell us your reaso
(It's OK to imagine unlikely or weird explanations;
don't be afraid to think outside the box.
Oddly enough, logic has very little to do with facts.)

Some examples:

ARGUMENT: I pulled an all-nighter studying for last week


test, and I ended up with an A. Tiredness must make me

COUNTER: What if you got an A because you actually stu


Or maybe the test was going to be easy for you all along

ARGUMENT: If I play with Dad's power tools, he'll yell at


But Dad is yelling at me for something. So I guess I must
played with the power tools.

COUNTER: What if he's yelling at you for a different reaso


scratching the car, or hammer-throwing the cat onto the

Season of mists and mellow


fruitfulness,

Close bosom-friend of the maturing


sun;

Conspiring with him how to load and bless

With fruit the vines that round the thatch-eves


run;

To bend with apples the moss'd cottagetrees,

And fill all fruit with ripeness to the


core;

To swell the gourd, and plump the hazel


shells

With a sweet kernel; to set budding


more,

And still more, later flowers for the bees,

Until they think warm days will never cease,

For Summer has o'er-brimm'd their clammy


cells.

Who hath not seen thee oft amid thy


store?

Sometimes whoever seeks abroad may


find

Thee sitting careless on a granary


floor,

Thy hair soft-lifted by the winnowing


wind

Or on a half-reapd furrow sound


asleep,

Drowsed with the fume of poppies, while thy hook

Spares the next swath and all its twind


flowers:

And sometimes like a gleaner thou dost keep

Steady thy laden head across a


brook;

Or by a cyder-press, with patient look,

Thou watchest the last oozings, hours by


hours.

Where are the songs of Spring? Ay, where are


they?

Think not of them, thou hast thy music too,

While barrd clouds bloom the soft-dying day

And touch the stubble-plains with rosy hue;

Then in a wailful choir the small gnats mourn

Among the river-sallows, borne


aloft

Or sinking as the light wind lives or


dies;
sinking

And full-grown lambs loud bleat from hilly bourn;

Hedge-crickets sing; and now with treble soft

The redbreast whistles from a gardencroft;

And gathering swallows twitter in the


skies.

S-ar putea să vă placă și