Friday, January 13, 2012

Defaulting message fields in protobuf

Not well documented is the technique for defaulting message fields in protobuf. Say you have a field of message type, named "start" here:

message Complex {
    required int64 real = 1;
    optional int64 imaginary = 2 [default = 0];
}

message Vector {
    required Complex start = 1;
    required int64 length = 2;
}

How to you provide a default value, say at the origin? Like this:

import "google/protobuf/descriptor.proto";

extend google.protobuf.FieldOptions {
    // Pick the field number that is right for you!
    optional Complex complex = 50000;
}

message Vector {
    optional Complex start = 1
    [(complex) = { real: 0 imaginary: 0 }];
    required int64 length = 2;
}

See the Custom Options section for details.

Wednesday, January 11, 2012

Write your own kernel

James Malloy will show you how!

Better than null

A rich, delightful post from Joel Neely on an alternative to returning null to signal "not found" or "No": have the caller supply a callback with appropriate methods for condition signaling. In Java:

interface Found {
    void present(final String name);

    void absent(final String name);
}

class Phonebook {
    private final Set<String> names = new HashSet<>;

    void add(final String name) {
        names.add(name);
    }

    void remove(final String name) {
        names.remove(name);
    }

    void find(final String name, final Found found) {
        if (names.contains(name))
            found.present(name);
        else
            found.absent(name);
    }
}

class LookupPhone extends Phone implements Found {
    private final Phonebook phonebook;

    LookupPhone(final Phonebook phonebook) {
        this.phonebook = phonebook;
    }

    @Override
    public void present(final String name) {
        ringUp(name);
    }

    @Override
    public void absent(final String name) {
        complainIsUnknown(name);
    }

    void placeCall(final String name) {
        phonebook.find(name, this);
    }
}

(Keyed directly into the editor: apologies for typos.)

Tuesday, January 10, 2012

Bill Bejeck writes on Guava EventBus. This is very similar to the "MagicBus" code I wrote for a client while at ThoughtWorks in the mid-naughts. Essentially it is an in-process event bus respecting Java method signatures rather than a string-based topic space. You can write decoupled code in a pub-sub style within your own application.

In some weird sense it is an implementation of the COMEFROM instruction. When a "caller" publishes the right parameter list, subscribing methods are invoked as if called directly. Adding new subscribers changes the meaning of the call site. The difference is that control returns to the publisher after all subscribers run; COMEFROM is a complete transfer of control and does not multiplex.

Long live INTERCAL!

Thursday, December 29, 2011

Friday, December 16, 2011

Linking profile to Google+

I've linked my Blogger profile to my Google+ account. Please comment if you have any thoughts on this, or if you see anything odd or missing as a result. Thanks!

Java server hiccups

Gil Tene at Azul has great insight into Java server performance gaffs. He calls them hiccups and provides tooling to find them.

Thursday, December 15, 2011

Monad in Java

The shortest, clearest description of monads in Java I have read:

public class OptionalMonad {
    public static <T> Optional<T> unit(T value) {
        return Optional.of(value);
    }

    public static <T, U> Optional<U> bind(
            Optional<T> value,
            Function<T, Optional<U>> function) {
        if (value.isPresent()) return function.apply(value.get());
        else return Optional.absent();
    }
}

Thanks to François Sarradin in a very nice post.

Friday, December 09, 2011

Netflix circuit breaker

Ben Schmaus of the Netflix team posts a lovely description of the degraded service features of their API. The real life use case is compelling. The dashboard is just brilliant.

Wednesday, November 30, 2011

The death of HyperCard, a whodunit

Stanislav Datskovskiy posts his theory on the murder of HyperCard. It wasn't the butler.

Baker's Treadmill and Disruptor

Flying Frog Consultancy in Cambridge, England posts a nice comparison between Disruptor and low-latency GC algorithms:

The core of [Disruptor's] idea is to accomplish all of this message passing using a single shared data structure, the disruptor, rather than using several separate concurrent queues.

Baker's Treadmill is a low-latency garbage collection algorithm. Allocated blocks in the heap are linked together to form a cyclic doubly-linked list that is divided into four segments using four iterators that chase each other around the ring as heap blocks are allocated, traced and freed.

Cleverly observed with a good diagram. As they say, read the whole thing.

Tuesday, November 22, 2011

Microbenchmarking and JVM magic

Martin Thompson posts on what I call microbenchmarking and JVM magic, Biased Locking, OSR, and Benchmarking Fun. It is a good praticum to Cliff Click's What the heck is OSR and why is it Bad (or Good)?. OSR is the kind of JVM magic that both surprises and frightens, but makes perfect sense. I walked a colleague through OSR and he agreed with a shrug: the thing you are glad someone else wrote, excess hair.

Colebourne on Scala: EJB2 redux?

Stephen Colebourne posts a lengthy critique of Scala, Scala feels like EJB2. Scala attracts my inner physicist but brings concern to my practical programmer. Colebourne makes these implicit thoughts explicit.

Friday, November 18, 2011

Defeating SSL certificate validation in Java

This post is a hack. You have been warned.

My employer swaps out SSL certificates traversing the corporate firewall. It is a financial firm with regulatory responsibilities that require monitoring all traffic leaving the company, including encrypted traffic.

One side effect is trouble with well-meaning programs which validate SSL certificates before trusting an encrypted connection. These validations fail using the self-signed replacement certificate.

Maven is such a tool:

[WARNING] Could not transfer metadata com.devspan.vendor.envjs:envjs-rhino/maven-metadata.xml from/to sonatype-oss (https://oss.sonatype.org/content/groups/public): Error transferring file: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

The remark about "PKIX path building failed" is characteristic of the probelm.

There is lots of good advice on fixing this correctly, say, here, here or here. But none of them work just now; I cannot edit the locked-down trust store for Java, it does not already contain the aforementioned replacement certificate, and I do not want to build a custom Maven to work around the problem.

Enter the hack.

$ mvn -Djavax.net.ssl.trustStore=/dev/null package

Tuesday, November 08, 2011

Martin Thompson on lock cost

Anything Martin Thompson says about performance is usually worth a good study. No exception is Locks & Condition Variables - Latency Impact.

His takeaway points:
  1. The one-way latency to signal a change is pretty much the same as what is considered current state of the art for network hops between nodes via a switch.
  2. The impact is clear when letting the OS choose what CPUs the threads get scheduled on rather than pinning them manually.

Azul's approach for low-latency Java servers

I no longer work in the low-latency space. Still Azul's completion of a non-VM "pause-less" JVM for Linux is exciting. (Also running a JVM on a VM was too recursive.) May it be a benefit to those who need low-latency java servers.

Whither JDK8?

Stephen Colebourne posts on the future of JDK8 based on current JEPs. Among the possiblities, I'm looking forward to:

Monday, November 07, 2011

Punk rock languages

I love Chris Adamson's description of Punk Rock Languages. His introduction should get your blood flowing:

That C has won the end-user practicality battle is obvious to everyone except developers.

The year is 1978, and the first wave of punk rock is reaching its nihilistic peak with infamous U.K. band the Sex Pistols touring the United States and promptly breaking up by the time they reach the West Coast. Elsewhere, Brian Kernighan and Dennis Ritchie are putting the finishing touches on their book The C Programming Language, which will become the de facto standardization of the language for years. While totally unrelated, these two events share a common bond: the ethos of both punk rock and C have lasted for decades, longer than anyone in 1978 could possibly have imagined.

And in many important ways, C is the programmer’s punk rock: it’s fast, messy, dangerous, and perfectly willing to kick your ass, but it’s also an ideal antidote to the pretensions and vanities that plague so many new programming languages. In an era of virtual machines and managed environments, C is the original Punk Rock Language.

Peregrine: Help for programming concurrency

Good news from Columbia University. Peregrine is automated software to aid in programming concurrency. More in the ACM: Major Breakthrough Improves Software Reliability and Security:

"Our main finding in developing Peregrine is that we can make threads deterministic in an efficient and stable way: Peregrine can compute a plan for allowing when and where a thread can 'change lanes' and can then place barriers between the lanes, allowing threads to change lanes only at fixed locations, following a fixed order," says Columbia professor Junfeng Yang. "Once Peregrine computes a good plan without collisions for one group of threads, it can reuse the plan on subsequent groups to avoid the cost of computing a new plan for each new group."

Friday, October 28, 2011

Herb Sutter: Elements of Modern C++ Style

The always readable Herb Sutter begins Elements of Modern C++ Style with:

The C++11 standard offers many useful new features. This page focuses specifically and only on those features that make C++11 really feel like a new language compared to C++98, because:

  • They change the styles and idioms you’ll use when writing C++ code, often including the way you’ll design C++ libraries. For example, you’ll see more smart pointer parameters and return values, and functions that return big objects by value.
  • They will be used so pervasively that you’ll probably see them in most code examples. For example, virtually every five-line modern C++ code example will say “auto” somewhere.

Use the other great C++11 features too. But get used to these ones first, because these are the pervasive ones that show why C++11 code is clean, safe, and fast – just as clean and safe as code written in any other modern mainstream language, and with C++’s traditional to-the-metal performance as strong as ever.

This sounds good.

Thursday, October 20, 2011

Why does blogger republish my posts so often?

Watching the ThoughtWorks Alumni aggregator feed, I see my own posts reappear frequently in batches. I'll see posts last edited days ago suddenly repost. Any suggestion on stopping this misbehavior would be appreciated.

Cheers,
--binkley